gpib_os.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. // SPDX-License-Identifier: GPL-2.0
  2. /***************************************************************************
  3. * copyright : (C) 2001, 2004 by Frank Mori Hess
  4. ***************************************************************************
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #define dev_fmt pr_fmt
  8. #include "ibsys.h"
  9. #include <linux/module.h>
  10. #include <linux/wait.h>
  11. #include <linux/list.h>
  12. #include <linux/fs.h>
  13. #include <linux/pci.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/string.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/kmod.h>
  20. #include <linux/uaccess.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_DESCRIPTION("GPIB base support");
  23. MODULE_ALIAS_CHARDEV_MAJOR(GPIB_CODE);
  24. static int board_type_ioctl(struct gpib_file_private *file_priv,
  25. struct gpib_board *board, unsigned long arg);
  26. static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  27. unsigned long arg);
  28. static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  29. unsigned long arg);
  30. static int command_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  31. unsigned long arg);
  32. static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg);
  33. static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg);
  34. static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg);
  35. static int wait_ioctl(struct gpib_file_private *file_priv,
  36. struct gpib_board *board, unsigned long arg);
  37. static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg);
  38. static int online_ioctl(struct gpib_board *board, unsigned long arg);
  39. static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg);
  40. static int take_control_ioctl(struct gpib_board *board, unsigned long arg);
  41. static int line_status_ioctl(struct gpib_board *board, unsigned long arg);
  42. static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  43. unsigned long arg);
  44. static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  45. unsigned long arg);
  46. static int eos_ioctl(struct gpib_board *board, unsigned long arg);
  47. static int request_service_ioctl(struct gpib_board *board, unsigned long arg);
  48. static int request_service2_ioctl(struct gpib_board *board, unsigned long arg);
  49. static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg);
  50. static int irq_ioctl(struct gpib_board_config *config, unsigned long arg);
  51. static int dma_ioctl(struct gpib_board_config *config, unsigned long arg);
  52. static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  53. unsigned long arg);
  54. static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  55. unsigned long arg);
  56. static int timeout_ioctl(struct gpib_board *board, unsigned long arg);
  57. static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg);
  58. static int board_info_ioctl(const struct gpib_board *board, unsigned long arg);
  59. static int ppc_ioctl(struct gpib_board *board, unsigned long arg);
  60. static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg);
  61. static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg);
  62. static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg);
  63. static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg);
  64. static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg);
  65. static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg);
  66. static int event_ioctl(struct gpib_board *board, unsigned long arg);
  67. static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg);
  68. static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg);
  69. static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board);
  70. static int pop_gpib_event_nolock(struct gpib_board *board,
  71. struct gpib_event_queue *queue, short *event_type);
  72. /*
  73. * Timer functions
  74. */
  75. /* Watchdog timeout routine */
  76. static void watchdog_timeout(struct timer_list *t)
  77. {
  78. struct gpib_board *board = timer_container_of(board, t, timer);
  79. set_bit(TIMO_NUM, &board->status);
  80. wake_up_interruptible(&board->wait);
  81. }
  82. /* install timer interrupt handler */
  83. void os_start_timer(struct gpib_board *board, unsigned int usec_timeout)
  84. /* Starts the timeout task */
  85. {
  86. if (timer_pending(&board->timer)) {
  87. dev_err(board->gpib_dev, "bug! timer already running?\n");
  88. return;
  89. }
  90. clear_bit(TIMO_NUM, &board->status);
  91. if (usec_timeout > 0) {
  92. board->timer.function = watchdog_timeout;
  93. /* set number of ticks */
  94. mod_timer(&board->timer, jiffies + usec_to_jiffies(usec_timeout));
  95. }
  96. }
  97. void os_remove_timer(struct gpib_board *board)
  98. /* Removes the timeout task */
  99. {
  100. if (timer_pending(&board->timer))
  101. timer_delete_sync(&board->timer);
  102. }
  103. int io_timed_out(struct gpib_board *board)
  104. {
  105. if (test_bit(TIMO_NUM, &board->status))
  106. return 1;
  107. return 0;
  108. }
  109. /*
  110. * this is a function instead of a constant because of Suse
  111. * defining HZ to be a function call to get_hz()
  112. */
  113. static inline int pseudo_irq_period(void)
  114. {
  115. return (HZ + 99) / 100;
  116. }
  117. static void pseudo_irq_handler(struct timer_list *t)
  118. {
  119. struct gpib_pseudo_irq *pseudo_irq = timer_container_of(pseudo_irq, t,
  120. timer);
  121. if (pseudo_irq->handler)
  122. pseudo_irq->handler(0, pseudo_irq->board);
  123. else
  124. pr_err("gpib: bug! pseudo_irq.handler is NULL\n");
  125. if (atomic_read(&pseudo_irq->active))
  126. mod_timer(&pseudo_irq->timer, jiffies + pseudo_irq_period());
  127. }
  128. int gpib_request_pseudo_irq(struct gpib_board *board, irqreturn_t (*handler)(int, void *))
  129. {
  130. if (timer_pending(&board->pseudo_irq.timer) || board->pseudo_irq.handler) {
  131. dev_err(board->gpib_dev, "only one pseudo interrupt per board allowed\n");
  132. return -1;
  133. }
  134. board->pseudo_irq.handler = handler;
  135. board->pseudo_irq.timer.function = pseudo_irq_handler;
  136. board->pseudo_irq.board = board;
  137. atomic_set(&board->pseudo_irq.active, 1);
  138. mod_timer(&board->pseudo_irq.timer, jiffies + pseudo_irq_period());
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(gpib_request_pseudo_irq);
  142. void gpib_free_pseudo_irq(struct gpib_board *board)
  143. {
  144. atomic_set(&board->pseudo_irq.active, 0);
  145. timer_delete_sync(&board->pseudo_irq.timer);
  146. board->pseudo_irq.handler = NULL;
  147. }
  148. EXPORT_SYMBOL(gpib_free_pseudo_irq);
  149. static const unsigned int serial_timeout = 1000000;
  150. unsigned int num_status_bytes(const struct gpib_status_queue *dev)
  151. {
  152. if (!dev)
  153. return 0;
  154. return dev->num_status_bytes;
  155. }
  156. // push status byte onto back of status byte fifo
  157. int push_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 poll_byte)
  158. {
  159. struct list_head *head = &device->status_bytes;
  160. struct gpib_status_byte *status;
  161. static const unsigned int max_num_status_bytes = 1024;
  162. int retval;
  163. if (num_status_bytes(device) >= max_num_status_bytes) {
  164. u8 lost_byte;
  165. device->dropped_byte = 1;
  166. retval = pop_status_byte(board, device, &lost_byte);
  167. if (retval < 0)
  168. return retval;
  169. }
  170. status = kmalloc_obj(*status);
  171. if (!status)
  172. return -ENOMEM;
  173. INIT_LIST_HEAD(&status->list);
  174. status->poll_byte = poll_byte;
  175. list_add_tail(&status->list, head);
  176. device->num_status_bytes++;
  177. dev_dbg(board->gpib_dev, "pushed status byte 0x%x, %i in queue\n",
  178. (int)poll_byte, num_status_bytes(device));
  179. return 0;
  180. }
  181. // pop status byte from front of status byte fifo
  182. int pop_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 *poll_byte)
  183. {
  184. struct list_head *head = &device->status_bytes;
  185. struct list_head *front = head->next;
  186. struct gpib_status_byte *status;
  187. if (num_status_bytes(device) == 0)
  188. return -EIO;
  189. if (front == head)
  190. return -EIO;
  191. if (device->dropped_byte) {
  192. device->dropped_byte = 0;
  193. return -EPIPE;
  194. }
  195. status = list_entry(front, struct gpib_status_byte, list);
  196. *poll_byte = status->poll_byte;
  197. list_del(front);
  198. kfree(status);
  199. device->num_status_bytes--;
  200. dev_dbg(board->gpib_dev, "popped status byte 0x%x, %i in queue\n",
  201. (int)*poll_byte, num_status_bytes(device));
  202. return 0;
  203. }
  204. struct gpib_status_queue *get_gpib_status_queue(struct gpib_board *board, unsigned int pad, int sad)
  205. {
  206. struct gpib_status_queue *device;
  207. struct list_head *list_ptr;
  208. const struct list_head *head = &board->device_list;
  209. for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) {
  210. device = list_entry(list_ptr, struct gpib_status_queue, list);
  211. if (gpib_address_equal(device->pad, device->sad, pad, sad))
  212. return device;
  213. }
  214. return NULL;
  215. }
  216. int get_serial_poll_byte(struct gpib_board *board, unsigned int pad, int sad,
  217. unsigned int usec_timeout, u8 *poll_byte)
  218. {
  219. struct gpib_status_queue *device;
  220. device = get_gpib_status_queue(board, pad, sad);
  221. if (num_status_bytes(device))
  222. return pop_status_byte(board, device, poll_byte);
  223. else
  224. return dvrsp(board, pad, sad, usec_timeout, poll_byte);
  225. }
  226. int autopoll_all_devices(struct gpib_board *board)
  227. {
  228. int retval;
  229. if (mutex_lock_interruptible(&board->user_mutex))
  230. return -ERESTARTSYS;
  231. if (mutex_lock_interruptible(&board->big_gpib_mutex)) {
  232. mutex_unlock(&board->user_mutex);
  233. return -ERESTARTSYS;
  234. }
  235. dev_dbg(board->gpib_dev, "autopoll has board lock\n");
  236. retval = serial_poll_all(board, serial_timeout);
  237. if (retval < 0) {
  238. mutex_unlock(&board->big_gpib_mutex);
  239. mutex_unlock(&board->user_mutex);
  240. return retval;
  241. }
  242. dev_dbg(board->gpib_dev, "complete\n");
  243. /*
  244. * need to wake wait queue in case someone is
  245. * waiting on RQS
  246. */
  247. wake_up_interruptible(&board->wait);
  248. mutex_unlock(&board->big_gpib_mutex);
  249. mutex_unlock(&board->user_mutex);
  250. return retval;
  251. }
  252. static int setup_serial_poll(struct gpib_board *board, unsigned int usec_timeout)
  253. {
  254. u8 cmd_string[8];
  255. int i;
  256. size_t bytes_written;
  257. int ret;
  258. os_start_timer(board, usec_timeout);
  259. ret = ibcac(board, 1, 1);
  260. if (ret < 0) {
  261. os_remove_timer(board);
  262. return ret;
  263. }
  264. i = 0;
  265. cmd_string[i++] = UNL;
  266. cmd_string[i++] = MLA(board->pad); /* controller's listen address */
  267. if (board->sad >= 0)
  268. cmd_string[i++] = MSA(board->sad);
  269. cmd_string[i++] = SPE; // serial poll enable
  270. ret = board->interface->command(board, cmd_string, i, &bytes_written);
  271. if (ret < 0 || bytes_written < i) {
  272. dev_dbg(board->gpib_dev, "failed to setup serial poll\n");
  273. os_remove_timer(board);
  274. return -EIO;
  275. }
  276. os_remove_timer(board);
  277. return 0;
  278. }
  279. static int read_serial_poll_byte(struct gpib_board *board, unsigned int pad,
  280. int sad, unsigned int usec_timeout, u8 *result)
  281. {
  282. u8 cmd_string[8];
  283. int end_flag;
  284. int ret;
  285. int i;
  286. size_t nbytes;
  287. dev_dbg(board->gpib_dev, "entering pad=%i sad=%i\n", pad, sad);
  288. os_start_timer(board, usec_timeout);
  289. ret = ibcac(board, 1, 1);
  290. if (ret < 0) {
  291. os_remove_timer(board);
  292. return ret;
  293. }
  294. i = 0;
  295. // send talk address
  296. cmd_string[i++] = MTA(pad);
  297. if (sad >= 0)
  298. cmd_string[i++] = MSA(sad);
  299. ret = board->interface->command(board, cmd_string, i, &nbytes);
  300. if (ret < 0 || nbytes < i) {
  301. dev_err(board->gpib_dev, "failed to setup serial poll\n");
  302. os_remove_timer(board);
  303. return -EIO;
  304. }
  305. ibgts(board);
  306. // read poll result
  307. ret = board->interface->read(board, result, 1, &end_flag, &nbytes);
  308. if (ret < 0 || nbytes < 1) {
  309. dev_err(board->gpib_dev, "serial poll failed\n");
  310. os_remove_timer(board);
  311. return -EIO;
  312. }
  313. os_remove_timer(board);
  314. return 0;
  315. }
  316. static int cleanup_serial_poll(struct gpib_board *board, unsigned int usec_timeout)
  317. {
  318. u8 cmd_string[8];
  319. int ret;
  320. size_t bytes_written;
  321. os_start_timer(board, usec_timeout);
  322. ret = ibcac(board, 1, 1);
  323. if (ret < 0) {
  324. os_remove_timer(board);
  325. return ret;
  326. }
  327. cmd_string[0] = SPD; /* disable serial poll bytes */
  328. cmd_string[1] = UNT;
  329. ret = board->interface->command(board, cmd_string, 2, &bytes_written);
  330. if (ret < 0 || bytes_written < 2) {
  331. dev_err(board->gpib_dev, "failed to disable serial poll\n");
  332. os_remove_timer(board);
  333. return -EIO;
  334. }
  335. os_remove_timer(board);
  336. return 0;
  337. }
  338. static int serial_poll_single(struct gpib_board *board, unsigned int pad, int sad,
  339. unsigned int usec_timeout, u8 *result)
  340. {
  341. int retval, cleanup_retval;
  342. retval = setup_serial_poll(board, usec_timeout);
  343. if (retval < 0)
  344. return retval;
  345. retval = read_serial_poll_byte(board, pad, sad, usec_timeout, result);
  346. cleanup_retval = cleanup_serial_poll(board, usec_timeout);
  347. if (retval < 0)
  348. return retval;
  349. if (cleanup_retval < 0)
  350. return retval;
  351. return 0;
  352. }
  353. int serial_poll_all(struct gpib_board *board, unsigned int usec_timeout)
  354. {
  355. int retval = 0;
  356. struct list_head *cur;
  357. const struct list_head *head = NULL;
  358. struct gpib_status_queue *device;
  359. u8 result;
  360. unsigned int num_bytes = 0;
  361. head = &board->device_list;
  362. if (head->next == head)
  363. return 0;
  364. retval = setup_serial_poll(board, usec_timeout);
  365. if (retval < 0)
  366. return retval;
  367. for (cur = head->next; cur != head; cur = cur->next) {
  368. device = list_entry(cur, struct gpib_status_queue, list);
  369. retval = read_serial_poll_byte(board,
  370. device->pad, device->sad, usec_timeout, &result);
  371. if (retval < 0)
  372. continue;
  373. if (result & request_service_bit) {
  374. retval = push_status_byte(board, device, result);
  375. if (retval < 0)
  376. continue;
  377. num_bytes++;
  378. }
  379. }
  380. retval = cleanup_serial_poll(board, usec_timeout);
  381. if (retval < 0)
  382. return retval;
  383. return num_bytes;
  384. }
  385. /*
  386. * DVRSP
  387. * This function performs a serial poll of the device with primary
  388. * address pad and secondary address sad. If the device has no
  389. * secondary address, pass a negative number in for this argument. At the
  390. * end of a successful serial poll the response is returned in result.
  391. * SPD and UNT are sent at the completion of the poll.
  392. */
  393. int dvrsp(struct gpib_board *board, unsigned int pad, int sad,
  394. unsigned int usec_timeout, u8 *result)
  395. {
  396. int status = ibstatus(board);
  397. int retval;
  398. if ((status & CIC) == 0) {
  399. dev_err(board->gpib_dev, "not CIC during serial poll\n");
  400. return -1;
  401. }
  402. if (pad > MAX_GPIB_PRIMARY_ADDRESS || sad > MAX_GPIB_SECONDARY_ADDRESS || sad < -1) {
  403. dev_err(board->gpib_dev, "bad address for serial poll");
  404. return -1;
  405. }
  406. retval = serial_poll_single(board, pad, sad, usec_timeout, result);
  407. if (io_timed_out(board))
  408. retval = -ETIMEDOUT;
  409. return retval;
  410. }
  411. static struct gpib_descriptor *handle_to_descriptor(const struct gpib_file_private *file_priv,
  412. int handle)
  413. {
  414. if (handle < 0 || handle >= GPIB_MAX_NUM_DESCRIPTORS) {
  415. pr_err("gpib: invalid handle %i\n", handle);
  416. return NULL;
  417. }
  418. return file_priv->descriptors[handle];
  419. }
  420. static int init_gpib_file_private(struct gpib_file_private *priv)
  421. {
  422. memset(priv, 0, sizeof(*priv));
  423. atomic_set(&priv->holding_mutex, 0);
  424. priv->descriptors[0] = kmalloc_obj(struct gpib_descriptor);
  425. if (!priv->descriptors[0]) {
  426. pr_err("gpib: failed to allocate default board descriptor\n");
  427. return -ENOMEM;
  428. }
  429. init_gpib_descriptor(priv->descriptors[0]);
  430. priv->descriptors[0]->is_board = 1;
  431. mutex_init(&priv->descriptors_mutex);
  432. return 0;
  433. }
  434. int ibopen(struct inode *inode, struct file *filep)
  435. {
  436. unsigned int minor = iminor(inode);
  437. struct gpib_board *board;
  438. struct gpib_file_private *priv;
  439. if (minor >= GPIB_MAX_NUM_BOARDS) {
  440. pr_err("gpib: invalid minor number of device file\n");
  441. return -ENXIO;
  442. }
  443. board = &board_array[minor];
  444. filep->private_data = kmalloc_obj(struct gpib_file_private);
  445. if (!filep->private_data)
  446. return -ENOMEM;
  447. priv = filep->private_data;
  448. init_gpib_file_private((struct gpib_file_private *)filep->private_data);
  449. if (board->use_count == 0) {
  450. int retval;
  451. retval = request_module("gpib%i", minor);
  452. if (retval)
  453. dev_dbg(board->gpib_dev, "request module returned %i\n", retval);
  454. }
  455. if (board->interface) {
  456. if (!try_module_get(board->provider_module)) {
  457. dev_err(board->gpib_dev, "try_module_get() failed\n");
  458. return -EIO;
  459. }
  460. board->use_count++;
  461. priv->got_module = 1;
  462. }
  463. return 0;
  464. }
  465. int ibclose(struct inode *inode, struct file *filep)
  466. {
  467. unsigned int minor = iminor(inode);
  468. struct gpib_board *board;
  469. struct gpib_file_private *priv = filep->private_data;
  470. struct gpib_descriptor *desc;
  471. if (minor >= GPIB_MAX_NUM_BOARDS) {
  472. pr_err("gpib: invalid minor number of device file\n");
  473. return -ENODEV;
  474. }
  475. board = &board_array[minor];
  476. if (priv) {
  477. desc = handle_to_descriptor(priv, 0);
  478. if (desc) {
  479. if (desc->autopoll_enabled) {
  480. dev_dbg(board->gpib_dev, "decrementing autospollers\n");
  481. if (board->autospollers > 0)
  482. board->autospollers--;
  483. else
  484. dev_err(board->gpib_dev,
  485. "Attempt to decrement zero autospollers\n");
  486. }
  487. } else {
  488. dev_err(board->gpib_dev, "Unexpected null gpib_descriptor\n");
  489. }
  490. cleanup_open_devices(priv, board);
  491. if (atomic_read(&priv->holding_mutex))
  492. mutex_unlock(&board->user_mutex);
  493. if (priv->got_module && board->use_count) {
  494. module_put(board->provider_module);
  495. --board->use_count;
  496. }
  497. kfree(filep->private_data);
  498. filep->private_data = NULL;
  499. }
  500. return 0;
  501. }
  502. long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  503. {
  504. unsigned int minor = iminor(file_inode(filep));
  505. struct gpib_board *board;
  506. struct gpib_file_private *file_priv = filep->private_data;
  507. long retval = -ENOTTY;
  508. if (minor >= GPIB_MAX_NUM_BOARDS) {
  509. pr_err("gpib: invalid minor number of device file\n");
  510. return -ENODEV;
  511. }
  512. board = &board_array[minor];
  513. if (mutex_lock_interruptible(&board->big_gpib_mutex))
  514. return -ERESTARTSYS;
  515. dev_dbg(board->gpib_dev, "ioctl %d, interface=%s, use=%d, onl=%d\n",
  516. cmd & 0xff,
  517. board->interface ? board->interface->name : "",
  518. board->use_count,
  519. board->online);
  520. switch (cmd) {
  521. case CFCBOARDTYPE:
  522. retval = board_type_ioctl(file_priv, board, arg);
  523. goto done;
  524. case IBONL:
  525. retval = online_ioctl(board, arg);
  526. goto done;
  527. default:
  528. break;
  529. }
  530. if (!board->interface) {
  531. dev_err(board->gpib_dev, "no gpib board configured\n");
  532. retval = -ENODEV;
  533. goto done;
  534. }
  535. if (file_priv->got_module == 0) {
  536. if (!try_module_get(board->provider_module)) {
  537. dev_err(board->gpib_dev, "try_module_get() failed\n");
  538. retval = -EIO;
  539. goto done;
  540. }
  541. file_priv->got_module = 1;
  542. board->use_count++;
  543. }
  544. switch (cmd) {
  545. case CFCBASE:
  546. retval = iobase_ioctl(&board->config, arg);
  547. goto done;
  548. case CFCIRQ:
  549. retval = irq_ioctl(&board->config, arg);
  550. goto done;
  551. case CFCDMA:
  552. retval = dma_ioctl(&board->config, arg);
  553. goto done;
  554. case IBAUTOSPOLL:
  555. retval = autospoll_ioctl(board, file_priv, arg);
  556. goto done;
  557. case IBBOARD_INFO:
  558. retval = board_info_ioctl(board, arg);
  559. goto done;
  560. case IBMUTEX:
  561. /*
  562. * Need to unlock board->big_gpib_mutex before potentially locking board->user_mutex
  563. * to maintain consistent locking order
  564. */
  565. mutex_unlock(&board->big_gpib_mutex);
  566. return mutex_ioctl(board, file_priv, arg);
  567. case IBPAD:
  568. retval = pad_ioctl(board, file_priv, arg);
  569. goto done;
  570. case IBSAD:
  571. retval = sad_ioctl(board, file_priv, arg);
  572. goto done;
  573. case IBSELECT_PCI:
  574. retval = select_pci_ioctl(&board->config, arg);
  575. goto done;
  576. case IBSELECT_DEVICE_PATH:
  577. retval = select_device_path_ioctl(&board->config, arg);
  578. goto done;
  579. default:
  580. break;
  581. }
  582. if (!board->online) {
  583. retval = -EINVAL;
  584. goto done;
  585. }
  586. switch (cmd) {
  587. case IBEVENT:
  588. retval = event_ioctl(board, arg);
  589. goto done;
  590. case IBCLOSEDEV:
  591. retval = close_dev_ioctl(filep, board, arg);
  592. goto done;
  593. case IBOPENDEV:
  594. retval = open_dev_ioctl(filep, board, arg);
  595. goto done;
  596. case IBSPOLL_BYTES:
  597. retval = status_bytes_ioctl(board, arg);
  598. goto done;
  599. case IBWAIT:
  600. retval = wait_ioctl(file_priv, board, arg);
  601. if (retval == -ERESTARTSYS)
  602. return retval;
  603. goto done;
  604. case IBLINES:
  605. retval = line_status_ioctl(board, arg);
  606. goto done;
  607. case IBLOC:
  608. board->interface->return_to_local(board);
  609. retval = 0;
  610. goto done;
  611. default:
  612. break;
  613. }
  614. spin_lock(&board->locking_pid_spinlock);
  615. if (current->pid != board->locking_pid) {
  616. spin_unlock(&board->locking_pid_spinlock);
  617. retval = -EPERM;
  618. goto done;
  619. }
  620. spin_unlock(&board->locking_pid_spinlock);
  621. switch (cmd) {
  622. case IB_T1_DELAY:
  623. retval = t1_delay_ioctl(board, arg);
  624. goto done;
  625. case IBCAC:
  626. retval = take_control_ioctl(board, arg);
  627. goto done;
  628. case IBCMD:
  629. /*
  630. * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
  631. * before we call them.
  632. */
  633. mutex_unlock(&board->big_gpib_mutex);
  634. return command_ioctl(file_priv, board, arg);
  635. case IBEOS:
  636. retval = eos_ioctl(board, arg);
  637. goto done;
  638. case IBGTS:
  639. retval = ibgts(board);
  640. goto done;
  641. case IBPPC:
  642. retval = ppc_ioctl(board, arg);
  643. goto done;
  644. case IBPP2_SET:
  645. retval = set_local_ppoll_mode_ioctl(board, arg);
  646. goto done;
  647. case IBPP2_GET:
  648. retval = get_local_ppoll_mode_ioctl(board, arg);
  649. goto done;
  650. case IBQUERY_BOARD_RSV:
  651. retval = query_board_rsv_ioctl(board, arg);
  652. goto done;
  653. case IBRD:
  654. /*
  655. * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
  656. * before we call them.
  657. */
  658. mutex_unlock(&board->big_gpib_mutex);
  659. return read_ioctl(file_priv, board, arg);
  660. case IBRPP:
  661. retval = parallel_poll_ioctl(board, arg);
  662. goto done;
  663. case IBRSC:
  664. retval = request_system_control_ioctl(board, arg);
  665. goto done;
  666. case IBRSP:
  667. retval = serial_poll_ioctl(board, arg);
  668. goto done;
  669. case IBRSV:
  670. retval = request_service_ioctl(board, arg);
  671. goto done;
  672. case IBRSV2:
  673. retval = request_service2_ioctl(board, arg);
  674. goto done;
  675. case IBSIC:
  676. retval = interface_clear_ioctl(board, arg);
  677. goto done;
  678. case IBSRE:
  679. retval = remote_enable_ioctl(board, arg);
  680. goto done;
  681. case IBTMO:
  682. retval = timeout_ioctl(board, arg);
  683. goto done;
  684. case IBWRT:
  685. /*
  686. * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex
  687. * before we call them.
  688. */
  689. mutex_unlock(&board->big_gpib_mutex);
  690. return write_ioctl(file_priv, board, arg);
  691. default:
  692. retval = -ENOTTY;
  693. goto done;
  694. }
  695. done:
  696. mutex_unlock(&board->big_gpib_mutex);
  697. dev_dbg(board->gpib_dev, "ioctl done status = 0x%lx\n", board->status);
  698. return retval;
  699. }
  700. static int board_type_ioctl(struct gpib_file_private *file_priv,
  701. struct gpib_board *board, unsigned long arg)
  702. {
  703. struct list_head *list_ptr;
  704. struct gpib_board_type_ioctl cmd;
  705. int retval;
  706. if (!capable(CAP_SYS_ADMIN))
  707. return -EPERM;
  708. if (board->online)
  709. return -EBUSY;
  710. retval = copy_from_user(&cmd, (void __user *)arg,
  711. sizeof(struct gpib_board_type_ioctl));
  712. if (retval)
  713. return -EFAULT;
  714. for (list_ptr = registered_drivers.next; list_ptr != &registered_drivers;
  715. list_ptr = list_ptr->next) {
  716. struct gpib_interface_list *entry;
  717. entry = list_entry(list_ptr, struct gpib_interface_list, list);
  718. if (strcmp(entry->interface->name, cmd.name) == 0) {
  719. int i;
  720. int had_module = file_priv->got_module;
  721. if (board->use_count) {
  722. for (i = 0; i < board->use_count; ++i)
  723. module_put(board->provider_module);
  724. board->interface = NULL;
  725. file_priv->got_module = 0;
  726. }
  727. board->interface = entry->interface;
  728. board->provider_module = entry->module;
  729. for (i = 0; i < board->use_count; ++i) {
  730. if (!try_module_get(entry->module)) {
  731. board->use_count = i;
  732. return -EIO;
  733. }
  734. }
  735. if (had_module == 0) {
  736. if (!try_module_get(entry->module))
  737. return -EIO;
  738. ++board->use_count;
  739. }
  740. file_priv->got_module = 1;
  741. return 0;
  742. }
  743. }
  744. return -EINVAL;
  745. }
  746. static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  747. unsigned long arg)
  748. {
  749. struct gpib_read_write_ioctl read_cmd;
  750. u8 __user *userbuf;
  751. unsigned long remain;
  752. int end_flag = 0;
  753. int retval;
  754. ssize_t read_ret = 0;
  755. struct gpib_descriptor *desc;
  756. size_t nbytes;
  757. retval = copy_from_user(&read_cmd, (void __user *)arg, sizeof(read_cmd));
  758. if (retval)
  759. return -EFAULT;
  760. if (read_cmd.completed_transfer_count > read_cmd.requested_transfer_count)
  761. return -EINVAL;
  762. if (WARN_ON_ONCE(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr)))
  763. return -EFAULT;
  764. userbuf = (u8 __user *)(unsigned long)read_cmd.buffer_ptr;
  765. userbuf += read_cmd.completed_transfer_count;
  766. remain = read_cmd.requested_transfer_count - read_cmd.completed_transfer_count;
  767. /* Check write access to buffer */
  768. if (!access_ok(userbuf, remain))
  769. return -EFAULT;
  770. /* Lock descriptors to prevent concurrent close from freeing descriptor */
  771. if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
  772. return -ERESTARTSYS;
  773. desc = handle_to_descriptor(file_priv, read_cmd.handle);
  774. if (!desc) {
  775. mutex_unlock(&file_priv->descriptors_mutex);
  776. return -EINVAL;
  777. }
  778. atomic_inc(&desc->descriptor_busy);
  779. mutex_unlock(&file_priv->descriptors_mutex);
  780. atomic_set(&desc->io_in_progress, 1);
  781. /* Read buffer loads till we fill the user supplied buffer */
  782. while (remain > 0 && end_flag == 0) {
  783. nbytes = 0;
  784. read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ?
  785. board->buffer_length : remain, &end_flag, &nbytes);
  786. if (nbytes == 0)
  787. break;
  788. retval = copy_to_user(userbuf, board->buffer, nbytes);
  789. if (retval) {
  790. retval = -EFAULT;
  791. break;
  792. }
  793. remain -= nbytes;
  794. userbuf += nbytes;
  795. if (read_ret < 0)
  796. break;
  797. }
  798. read_cmd.completed_transfer_count = read_cmd.requested_transfer_count - remain;
  799. read_cmd.end = end_flag;
  800. /*
  801. * suppress errors (for example due to timeout or interruption by device clear)
  802. * if all bytes got sent. This prevents races that can occur in the various drivers
  803. * if a device receives a device clear immediately after a transfer completes and
  804. * the driver code wasn't careful enough to handle that case.
  805. */
  806. if (remain == 0 || end_flag)
  807. read_ret = 0;
  808. if (retval == 0)
  809. retval = copy_to_user((void __user *)arg, &read_cmd, sizeof(read_cmd));
  810. atomic_set(&desc->io_in_progress, 0);
  811. atomic_dec(&desc->descriptor_busy);
  812. wake_up_interruptible(&board->wait);
  813. if (retval)
  814. return -EFAULT;
  815. return read_ret;
  816. }
  817. static int command_ioctl(struct gpib_file_private *file_priv,
  818. struct gpib_board *board, unsigned long arg)
  819. {
  820. struct gpib_read_write_ioctl cmd;
  821. u8 __user *userbuf;
  822. unsigned long remain;
  823. int retval;
  824. int fault = 0;
  825. struct gpib_descriptor *desc;
  826. size_t bytes_written;
  827. int no_clear_io_in_prog;
  828. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  829. if (retval)
  830. return -EFAULT;
  831. if (cmd.completed_transfer_count > cmd.requested_transfer_count)
  832. return -EINVAL;
  833. userbuf = (u8 __user *)(unsigned long)cmd.buffer_ptr;
  834. userbuf += cmd.completed_transfer_count;
  835. no_clear_io_in_prog = cmd.end;
  836. cmd.end = 0;
  837. remain = cmd.requested_transfer_count - cmd.completed_transfer_count;
  838. /* Check read access to buffer */
  839. if (!access_ok(userbuf, remain))
  840. return -EFAULT;
  841. /* Lock descriptors to prevent concurrent close from freeing descriptor */
  842. if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
  843. return -ERESTARTSYS;
  844. desc = handle_to_descriptor(file_priv, cmd.handle);
  845. if (!desc) {
  846. mutex_unlock(&file_priv->descriptors_mutex);
  847. return -EINVAL;
  848. }
  849. atomic_inc(&desc->descriptor_busy);
  850. mutex_unlock(&file_priv->descriptors_mutex);
  851. /*
  852. * Write buffer loads till we empty the user supplied buffer.
  853. * Call drivers at least once, even if remain is zero, in
  854. * order to allow them to insure previous commands were
  855. * completely finished, in the case of a restarted ioctl.
  856. */
  857. atomic_set(&desc->io_in_progress, 1);
  858. do {
  859. fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
  860. board->buffer_length : remain);
  861. if (fault) {
  862. retval = -EFAULT;
  863. bytes_written = 0;
  864. } else {
  865. retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ?
  866. board->buffer_length : remain, &bytes_written);
  867. }
  868. remain -= bytes_written;
  869. userbuf += bytes_written;
  870. if (retval < 0) {
  871. atomic_set(&desc->io_in_progress, 0);
  872. atomic_dec(&desc->descriptor_busy);
  873. wake_up_interruptible(&board->wait);
  874. break;
  875. }
  876. } while (remain > 0);
  877. cmd.completed_transfer_count = cmd.requested_transfer_count - remain;
  878. if (fault == 0)
  879. fault = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
  880. /*
  881. * no_clear_io_in_prog (cmd.end) is true when io_in_progress should
  882. * not be set to zero because the cmd in progress is the address setup
  883. * operation for an async read or write. This causes CMPL not to be set
  884. * in general_ibstatus until the async read or write completes.
  885. */
  886. if (!no_clear_io_in_prog || fault)
  887. atomic_set(&desc->io_in_progress, 0);
  888. atomic_dec(&desc->descriptor_busy);
  889. wake_up_interruptible(&board->wait);
  890. if (fault)
  891. return -EFAULT;
  892. return retval;
  893. }
  894. static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  895. unsigned long arg)
  896. {
  897. struct gpib_read_write_ioctl write_cmd;
  898. u8 __user *userbuf;
  899. unsigned long remain;
  900. int retval = 0;
  901. int fault;
  902. struct gpib_descriptor *desc;
  903. fault = copy_from_user(&write_cmd, (void __user *)arg, sizeof(write_cmd));
  904. if (fault)
  905. return -EFAULT;
  906. if (write_cmd.completed_transfer_count > write_cmd.requested_transfer_count)
  907. return -EINVAL;
  908. userbuf = (u8 __user *)(unsigned long)write_cmd.buffer_ptr;
  909. userbuf += write_cmd.completed_transfer_count;
  910. remain = write_cmd.requested_transfer_count - write_cmd.completed_transfer_count;
  911. /* Check read access to buffer */
  912. if (!access_ok(userbuf, remain))
  913. return -EFAULT;
  914. /* Lock descriptors to prevent concurrent close from freeing descriptor */
  915. if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
  916. return -ERESTARTSYS;
  917. desc = handle_to_descriptor(file_priv, write_cmd.handle);
  918. if (!desc) {
  919. mutex_unlock(&file_priv->descriptors_mutex);
  920. return -EINVAL;
  921. }
  922. atomic_inc(&desc->descriptor_busy);
  923. mutex_unlock(&file_priv->descriptors_mutex);
  924. atomic_set(&desc->io_in_progress, 1);
  925. /* Write buffer loads till we empty the user supplied buffer */
  926. while (remain > 0) {
  927. int send_eoi;
  928. size_t bytes_written = 0;
  929. send_eoi = remain <= board->buffer_length && write_cmd.end;
  930. fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ?
  931. board->buffer_length : remain);
  932. if (fault) {
  933. retval = -EFAULT;
  934. break;
  935. }
  936. retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ?
  937. board->buffer_length : remain, send_eoi, &bytes_written);
  938. remain -= bytes_written;
  939. userbuf += bytes_written;
  940. if (retval < 0)
  941. break;
  942. }
  943. write_cmd.completed_transfer_count = write_cmd.requested_transfer_count - remain;
  944. /*
  945. * suppress errors (for example due to timeout or interruption by device clear)
  946. * if all bytes got sent. This prevents races that can occur in the various drivers
  947. * if a device receives a device clear immediately after a transfer completes and
  948. * the driver code wasn't careful enough to handle that case.
  949. */
  950. if (remain == 0)
  951. retval = 0;
  952. if (fault == 0)
  953. fault = copy_to_user((void __user *)arg, &write_cmd, sizeof(write_cmd));
  954. atomic_set(&desc->io_in_progress, 0);
  955. atomic_dec(&desc->descriptor_busy);
  956. wake_up_interruptible(&board->wait);
  957. if (fault)
  958. return -EFAULT;
  959. return retval;
  960. }
  961. static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg)
  962. {
  963. struct gpib_status_queue *device;
  964. struct gpib_spoll_bytes_ioctl cmd;
  965. int retval;
  966. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  967. if (retval)
  968. return -EFAULT;
  969. device = get_gpib_status_queue(board, cmd.pad, cmd.sad);
  970. if (!device)
  971. cmd.num_bytes = 0;
  972. else
  973. cmd.num_bytes = num_status_bytes(device);
  974. retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
  975. if (retval)
  976. return -EFAULT;
  977. return 0;
  978. }
  979. static int increment_open_device_count(struct gpib_board *board, struct list_head *head,
  980. unsigned int pad, int sad)
  981. {
  982. struct list_head *list_ptr;
  983. struct gpib_status_queue *device;
  984. /*
  985. * first see if address has already been opened, then increment
  986. * open count
  987. */
  988. for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) {
  989. device = list_entry(list_ptr, struct gpib_status_queue, list);
  990. if (gpib_address_equal(device->pad, device->sad, pad, sad)) {
  991. dev_dbg(board->gpib_dev, "incrementing open count for pad %i, sad %i\n",
  992. device->pad, device->sad);
  993. device->reference_count++;
  994. return 0;
  995. }
  996. }
  997. /* otherwise we need to allocate a new struct gpib_status_queue */
  998. device = kmalloc_obj(struct gpib_status_queue, GFP_ATOMIC);
  999. if (!device)
  1000. return -ENOMEM;
  1001. init_gpib_status_queue(device);
  1002. device->pad = pad;
  1003. device->sad = sad;
  1004. device->reference_count = 1;
  1005. list_add(&device->list, head);
  1006. dev_dbg(board->gpib_dev, "opened pad %i, sad %i\n", device->pad, device->sad);
  1007. return 0;
  1008. }
  1009. static int subtract_open_device_count(struct gpib_board *board, struct list_head *head,
  1010. unsigned int pad, int sad, unsigned int count)
  1011. {
  1012. struct gpib_status_queue *device;
  1013. struct list_head *list_ptr;
  1014. for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) {
  1015. device = list_entry(list_ptr, struct gpib_status_queue, list);
  1016. if (gpib_address_equal(device->pad, device->sad, pad, sad)) {
  1017. dev_dbg(board->gpib_dev, "decrementing open count for pad %i, sad %i\n",
  1018. device->pad, device->sad);
  1019. if (count > device->reference_count) {
  1020. dev_err(board->gpib_dev, "bug! in %s()\n", __func__);
  1021. return -EINVAL;
  1022. }
  1023. device->reference_count -= count;
  1024. if (device->reference_count == 0) {
  1025. dev_dbg(board->gpib_dev, "closing pad %i, sad %i\n",
  1026. device->pad, device->sad);
  1027. list_del(list_ptr);
  1028. kfree(device);
  1029. }
  1030. return 0;
  1031. }
  1032. }
  1033. dev_err(board->gpib_dev, "bug! tried to close address that was never opened!\n");
  1034. return -EINVAL;
  1035. }
  1036. static inline int decrement_open_device_count(struct gpib_board *board, struct list_head *head,
  1037. unsigned int pad, int sad)
  1038. {
  1039. return subtract_open_device_count(board, head, pad, sad, 1);
  1040. }
  1041. static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board)
  1042. {
  1043. int retval = 0;
  1044. int i;
  1045. for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) {
  1046. struct gpib_descriptor *desc;
  1047. desc = file_priv->descriptors[i];
  1048. if (!desc)
  1049. continue;
  1050. if (desc->is_board == 0) {
  1051. retval = decrement_open_device_count(board, &board->device_list, desc->pad,
  1052. desc->sad);
  1053. if (retval < 0)
  1054. return retval;
  1055. }
  1056. kfree(desc);
  1057. file_priv->descriptors[i] = NULL;
  1058. }
  1059. return 0;
  1060. }
  1061. static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg)
  1062. {
  1063. struct gpib_open_dev_ioctl open_dev_cmd;
  1064. int retval;
  1065. struct gpib_file_private *file_priv = filep->private_data;
  1066. int i;
  1067. retval = copy_from_user(&open_dev_cmd, (void __user *)arg, sizeof(open_dev_cmd));
  1068. if (retval)
  1069. return -EFAULT;
  1070. if (mutex_lock_interruptible(&file_priv->descriptors_mutex))
  1071. return -ERESTARTSYS;
  1072. for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++)
  1073. if (!file_priv->descriptors[i])
  1074. break;
  1075. if (i == GPIB_MAX_NUM_DESCRIPTORS) {
  1076. mutex_unlock(&file_priv->descriptors_mutex);
  1077. return -ERANGE;
  1078. }
  1079. file_priv->descriptors[i] = kmalloc_obj(struct gpib_descriptor);
  1080. if (!file_priv->descriptors[i]) {
  1081. mutex_unlock(&file_priv->descriptors_mutex);
  1082. return -ENOMEM;
  1083. }
  1084. init_gpib_descriptor(file_priv->descriptors[i]);
  1085. file_priv->descriptors[i]->pad = open_dev_cmd.pad;
  1086. file_priv->descriptors[i]->sad = open_dev_cmd.sad;
  1087. file_priv->descriptors[i]->is_board = open_dev_cmd.is_board;
  1088. mutex_unlock(&file_priv->descriptors_mutex);
  1089. retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad,
  1090. open_dev_cmd.sad);
  1091. if (retval < 0)
  1092. return retval;
  1093. /*
  1094. * clear stuck srq state, since we may be able to find service request on
  1095. * the new device
  1096. */
  1097. atomic_set(&board->stuck_srq, 0);
  1098. open_dev_cmd.handle = i;
  1099. retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd));
  1100. if (retval)
  1101. return -EFAULT;
  1102. return 0;
  1103. }
  1104. static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg)
  1105. {
  1106. struct gpib_close_dev_ioctl cmd;
  1107. struct gpib_file_private *file_priv = filep->private_data;
  1108. struct gpib_descriptor *desc;
  1109. unsigned int pad;
  1110. int sad;
  1111. int retval;
  1112. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1113. if (retval)
  1114. return -EFAULT;
  1115. if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS)
  1116. return -EINVAL;
  1117. mutex_lock(&file_priv->descriptors_mutex);
  1118. desc = file_priv->descriptors[cmd.handle];
  1119. if (!desc) {
  1120. mutex_unlock(&file_priv->descriptors_mutex);
  1121. return -EINVAL;
  1122. }
  1123. if (atomic_read(&desc->descriptor_busy)) {
  1124. mutex_unlock(&file_priv->descriptors_mutex);
  1125. return -EBUSY;
  1126. }
  1127. /* Remove from table while holding lock to prevent new IO from starting */
  1128. file_priv->descriptors[cmd.handle] = NULL;
  1129. pad = desc->pad;
  1130. sad = desc->sad;
  1131. mutex_unlock(&file_priv->descriptors_mutex);
  1132. retval = decrement_open_device_count(board, &board->device_list, pad, sad);
  1133. kfree(desc);
  1134. return retval;
  1135. }
  1136. static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg)
  1137. {
  1138. struct gpib_serial_poll_ioctl serial_cmd;
  1139. int retval;
  1140. retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd));
  1141. if (retval)
  1142. return -EFAULT;
  1143. retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout,
  1144. &serial_cmd.status_byte);
  1145. if (retval < 0)
  1146. return retval;
  1147. retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd));
  1148. if (retval)
  1149. return -EFAULT;
  1150. return 0;
  1151. }
  1152. static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board,
  1153. unsigned long arg)
  1154. {
  1155. struct gpib_wait_ioctl wait_cmd;
  1156. int retval;
  1157. struct gpib_descriptor *desc;
  1158. retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd));
  1159. if (retval)
  1160. return -EFAULT;
  1161. /*
  1162. * Lock descriptors to prevent concurrent close from freeing
  1163. * descriptor. ibwait() releases big_gpib_mutex when wait_mask
  1164. * is non-zero, so desc must be pinned with descriptor_busy.
  1165. */
  1166. mutex_lock(&file_priv->descriptors_mutex);
  1167. desc = handle_to_descriptor(file_priv, wait_cmd.handle);
  1168. if (!desc) {
  1169. mutex_unlock(&file_priv->descriptors_mutex);
  1170. return -EINVAL;
  1171. }
  1172. atomic_inc(&desc->descriptor_busy);
  1173. mutex_unlock(&file_priv->descriptors_mutex);
  1174. retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask,
  1175. wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc);
  1176. atomic_dec(&desc->descriptor_busy);
  1177. if (retval < 0)
  1178. return retval;
  1179. retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd));
  1180. if (retval)
  1181. return -EFAULT;
  1182. return 0;
  1183. }
  1184. static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg)
  1185. {
  1186. u8 poll_byte;
  1187. int retval;
  1188. retval = ibrpp(board, &poll_byte);
  1189. if (retval < 0)
  1190. return retval;
  1191. retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte));
  1192. if (retval)
  1193. return -EFAULT;
  1194. return 0;
  1195. }
  1196. static int online_ioctl(struct gpib_board *board, unsigned long arg)
  1197. {
  1198. struct gpib_online_ioctl online_cmd;
  1199. int retval;
  1200. void __user *init_data = NULL;
  1201. board->config.init_data = NULL;
  1202. if (!capable(CAP_SYS_ADMIN))
  1203. return -EPERM;
  1204. retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd));
  1205. if (retval)
  1206. return -EFAULT;
  1207. if (online_cmd.init_data_length > 0) {
  1208. board->config.init_data = vmalloc(online_cmd.init_data_length);
  1209. if (!board->config.init_data)
  1210. return -ENOMEM;
  1211. if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr)))
  1212. return -EFAULT;
  1213. init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr);
  1214. retval = copy_from_user(board->config.init_data, init_data,
  1215. online_cmd.init_data_length);
  1216. if (retval) {
  1217. vfree(board->config.init_data);
  1218. return -EFAULT;
  1219. }
  1220. board->config.init_data_length = online_cmd.init_data_length;
  1221. } else {
  1222. board->config.init_data = NULL;
  1223. board->config.init_data_length = 0;
  1224. }
  1225. if (online_cmd.online)
  1226. retval = ibonline(board);
  1227. else
  1228. retval = iboffline(board);
  1229. if (board->config.init_data) {
  1230. vfree(board->config.init_data);
  1231. board->config.init_data = NULL;
  1232. board->config.init_data_length = 0;
  1233. }
  1234. return retval;
  1235. }
  1236. static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg)
  1237. {
  1238. int enable;
  1239. int retval;
  1240. retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable));
  1241. if (retval)
  1242. return -EFAULT;
  1243. return ibsre(board, enable);
  1244. }
  1245. static int take_control_ioctl(struct gpib_board *board, unsigned long arg)
  1246. {
  1247. int synchronous;
  1248. int retval;
  1249. retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous));
  1250. if (retval)
  1251. return -EFAULT;
  1252. return ibcac(board, synchronous, 1);
  1253. }
  1254. static int line_status_ioctl(struct gpib_board *board, unsigned long arg)
  1255. {
  1256. short lines;
  1257. int retval;
  1258. retval = iblines(board, &lines);
  1259. if (retval < 0)
  1260. return retval;
  1261. retval = copy_to_user((void __user *)arg, &lines, sizeof(lines));
  1262. if (retval)
  1263. return -EFAULT;
  1264. return 0;
  1265. }
  1266. static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  1267. unsigned long arg)
  1268. {
  1269. struct gpib_pad_ioctl cmd;
  1270. int retval;
  1271. struct gpib_descriptor *desc;
  1272. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1273. if (retval)
  1274. return -EFAULT;
  1275. desc = handle_to_descriptor(file_priv, cmd.handle);
  1276. if (!desc)
  1277. return -EINVAL;
  1278. if (desc->is_board) {
  1279. retval = ibpad(board, cmd.pad);
  1280. if (retval < 0)
  1281. return retval;
  1282. } else {
  1283. retval = decrement_open_device_count(board, &board->device_list, desc->pad,
  1284. desc->sad);
  1285. if (retval < 0)
  1286. return retval;
  1287. desc->pad = cmd.pad;
  1288. retval = increment_open_device_count(board, &board->device_list, desc->pad,
  1289. desc->sad);
  1290. if (retval < 0)
  1291. return retval;
  1292. }
  1293. return 0;
  1294. }
  1295. static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  1296. unsigned long arg)
  1297. {
  1298. struct gpib_sad_ioctl cmd;
  1299. int retval;
  1300. struct gpib_descriptor *desc;
  1301. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1302. if (retval)
  1303. return -EFAULT;
  1304. desc = handle_to_descriptor(file_priv, cmd.handle);
  1305. if (!desc)
  1306. return -EINVAL;
  1307. if (desc->is_board) {
  1308. retval = ibsad(board, cmd.sad);
  1309. if (retval < 0)
  1310. return retval;
  1311. } else {
  1312. retval = decrement_open_device_count(board, &board->device_list, desc->pad,
  1313. desc->sad);
  1314. if (retval < 0)
  1315. return retval;
  1316. desc->sad = cmd.sad;
  1317. retval = increment_open_device_count(board, &board->device_list, desc->pad,
  1318. desc->sad);
  1319. if (retval < 0)
  1320. return retval;
  1321. }
  1322. return 0;
  1323. }
  1324. static int eos_ioctl(struct gpib_board *board, unsigned long arg)
  1325. {
  1326. struct gpib_eos_ioctl eos_cmd;
  1327. int retval;
  1328. retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd));
  1329. if (retval)
  1330. return -EFAULT;
  1331. return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags);
  1332. }
  1333. static int request_service_ioctl(struct gpib_board *board, unsigned long arg)
  1334. {
  1335. u8 status_byte;
  1336. int retval;
  1337. retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte));
  1338. if (retval)
  1339. return -EFAULT;
  1340. return ibrsv2(board, status_byte, status_byte & request_service_bit);
  1341. }
  1342. static int request_service2_ioctl(struct gpib_board *board, unsigned long arg)
  1343. {
  1344. struct gpib_request_service2 request_service2_cmd;
  1345. int retval;
  1346. retval = copy_from_user(&request_service2_cmd, (void __user *)arg,
  1347. sizeof(struct gpib_request_service2));
  1348. if (retval)
  1349. return -EFAULT;
  1350. return ibrsv2(board, request_service2_cmd.status_byte,
  1351. request_service2_cmd.new_reason_for_service);
  1352. }
  1353. static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg)
  1354. {
  1355. u64 base_addr;
  1356. int retval;
  1357. if (!capable(CAP_SYS_ADMIN))
  1358. return -EPERM;
  1359. retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr));
  1360. if (retval)
  1361. return -EFAULT;
  1362. if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr)))
  1363. return -EFAULT;
  1364. config->ibbase = base_addr;
  1365. return 0;
  1366. }
  1367. static int irq_ioctl(struct gpib_board_config *config, unsigned long arg)
  1368. {
  1369. unsigned int irq;
  1370. int retval;
  1371. if (!capable(CAP_SYS_ADMIN))
  1372. return -EPERM;
  1373. retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq));
  1374. if (retval)
  1375. return -EFAULT;
  1376. config->ibirq = irq;
  1377. return 0;
  1378. }
  1379. static int dma_ioctl(struct gpib_board_config *config, unsigned long arg)
  1380. {
  1381. unsigned int dma_channel;
  1382. int retval;
  1383. if (!capable(CAP_SYS_ADMIN))
  1384. return -EPERM;
  1385. retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel));
  1386. if (retval)
  1387. return -EFAULT;
  1388. config->ibdma = dma_channel;
  1389. return 0;
  1390. }
  1391. static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  1392. unsigned long arg)
  1393. {
  1394. short enable;
  1395. int retval;
  1396. struct gpib_descriptor *desc;
  1397. retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable));
  1398. if (retval)
  1399. return -EFAULT;
  1400. desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */
  1401. if (enable) {
  1402. if (!desc->autopoll_enabled) {
  1403. board->autospollers++;
  1404. desc->autopoll_enabled = 1;
  1405. }
  1406. retval = 0;
  1407. } else {
  1408. if (desc->autopoll_enabled) {
  1409. desc->autopoll_enabled = 0;
  1410. if (board->autospollers > 0) {
  1411. board->autospollers--;
  1412. retval = 0;
  1413. } else {
  1414. dev_err(board->gpib_dev,
  1415. "tried to set number of autospollers negative\n");
  1416. retval = -EINVAL;
  1417. }
  1418. } else {
  1419. dev_err(board->gpib_dev, "autopoll disable requested before enable\n");
  1420. retval = -EINVAL;
  1421. }
  1422. }
  1423. return retval;
  1424. }
  1425. static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv,
  1426. unsigned long arg)
  1427. {
  1428. int retval, lock_mutex;
  1429. retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex));
  1430. if (retval)
  1431. return -EFAULT;
  1432. if (lock_mutex) {
  1433. retval = mutex_lock_interruptible(&board->user_mutex);
  1434. if (retval)
  1435. return -ERESTARTSYS;
  1436. spin_lock(&board->locking_pid_spinlock);
  1437. board->locking_pid = current->pid;
  1438. spin_unlock(&board->locking_pid_spinlock);
  1439. atomic_set(&file_priv->holding_mutex, 1);
  1440. dev_dbg(board->gpib_dev, "locked board mutex\n");
  1441. } else {
  1442. spin_lock(&board->locking_pid_spinlock);
  1443. if (current->pid != board->locking_pid) {
  1444. dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n",
  1445. current->pid, board->locking_pid);
  1446. spin_unlock(&board->locking_pid_spinlock);
  1447. return -EPERM;
  1448. }
  1449. board->locking_pid = 0;
  1450. spin_unlock(&board->locking_pid_spinlock);
  1451. atomic_set(&file_priv->holding_mutex, 0);
  1452. mutex_unlock(&board->user_mutex);
  1453. dev_dbg(board->gpib_dev, "unlocked board mutex\n");
  1454. }
  1455. return 0;
  1456. }
  1457. static int timeout_ioctl(struct gpib_board *board, unsigned long arg)
  1458. {
  1459. unsigned int timeout;
  1460. int retval;
  1461. retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout));
  1462. if (retval)
  1463. return -EFAULT;
  1464. board->usec_timeout = timeout;
  1465. dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout);
  1466. return 0;
  1467. }
  1468. static int ppc_ioctl(struct gpib_board *board, unsigned long arg)
  1469. {
  1470. struct gpib_ppoll_config_ioctl cmd;
  1471. int retval;
  1472. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1473. if (retval)
  1474. return -EFAULT;
  1475. if (cmd.set_ist) {
  1476. board->ist = 1;
  1477. board->interface->parallel_poll_response(board, board->ist);
  1478. } else if (cmd.clear_ist) {
  1479. board->ist = 0;
  1480. board->interface->parallel_poll_response(board, board->ist);
  1481. }
  1482. if (cmd.config) {
  1483. retval = ibppc(board, cmd.config);
  1484. if (retval < 0)
  1485. return retval;
  1486. }
  1487. return 0;
  1488. }
  1489. static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg)
  1490. {
  1491. short cmd;
  1492. int retval;
  1493. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1494. if (retval)
  1495. return -EFAULT;
  1496. if (!board->interface->local_parallel_poll_mode)
  1497. return -ENOENT;
  1498. board->local_ppoll_mode = cmd != 0;
  1499. board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode);
  1500. return 0;
  1501. }
  1502. static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg)
  1503. {
  1504. short cmd;
  1505. int retval;
  1506. cmd = board->local_ppoll_mode;
  1507. retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd));
  1508. if (retval)
  1509. return -EFAULT;
  1510. return 0;
  1511. }
  1512. static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg)
  1513. {
  1514. int status;
  1515. int retval;
  1516. status = board->interface->serial_poll_status(board);
  1517. retval = copy_to_user((void __user *)arg, &status, sizeof(status));
  1518. if (retval)
  1519. return -EFAULT;
  1520. return 0;
  1521. }
  1522. static int board_info_ioctl(const struct gpib_board *board, unsigned long arg)
  1523. {
  1524. struct gpib_board_info_ioctl info = { };
  1525. int retval;
  1526. info.pad = board->pad;
  1527. info.sad = board->sad;
  1528. info.parallel_poll_configuration = board->parallel_poll_configuration;
  1529. info.is_system_controller = board->master;
  1530. if (board->autospollers)
  1531. info.autopolling = 1;
  1532. else
  1533. info.autopolling = 0;
  1534. info.t1_delay = board->t1_nano_sec;
  1535. info.ist = board->ist;
  1536. info.no_7_bit_eos = board->interface->no_7_bit_eos;
  1537. retval = copy_to_user((void __user *)arg, &info, sizeof(info));
  1538. if (retval)
  1539. return -EFAULT;
  1540. return 0;
  1541. }
  1542. static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg)
  1543. {
  1544. unsigned int usec_duration;
  1545. int retval;
  1546. retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration));
  1547. if (retval)
  1548. return -EFAULT;
  1549. return ibsic(board, usec_duration);
  1550. }
  1551. static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg)
  1552. {
  1553. struct gpib_select_pci_ioctl selection;
  1554. int retval;
  1555. if (!capable(CAP_SYS_ADMIN))
  1556. return -EPERM;
  1557. retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection));
  1558. if (retval)
  1559. return -EFAULT;
  1560. config->pci_bus = selection.pci_bus;
  1561. config->pci_slot = selection.pci_slot;
  1562. return 0;
  1563. }
  1564. static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg)
  1565. {
  1566. struct gpib_select_device_path_ioctl *selection;
  1567. int retval;
  1568. if (!capable(CAP_SYS_ADMIN))
  1569. return -EPERM;
  1570. selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl));
  1571. if (!selection)
  1572. return -ENOMEM;
  1573. retval = copy_from_user(selection, (void __user *)arg,
  1574. sizeof(struct gpib_select_device_path_ioctl));
  1575. if (retval) {
  1576. vfree(selection);
  1577. return -EFAULT;
  1578. }
  1579. selection->device_path[sizeof(selection->device_path) - 1] = '\0';
  1580. kfree(config->device_path);
  1581. config->device_path = NULL;
  1582. if (strlen(selection->device_path) > 0)
  1583. config->device_path = kstrdup(selection->device_path, GFP_KERNEL);
  1584. vfree(selection);
  1585. return 0;
  1586. }
  1587. unsigned int num_gpib_events(const struct gpib_event_queue *queue)
  1588. {
  1589. return queue->num_events;
  1590. }
  1591. static int push_gpib_event_nolock(struct gpib_board *board, short event_type)
  1592. {
  1593. struct gpib_event_queue *queue = &board->event_queue;
  1594. struct list_head *head = &queue->event_head;
  1595. struct gpib_event *event;
  1596. static const unsigned int max_num_events = 1024;
  1597. int retval;
  1598. if (num_gpib_events(queue) >= max_num_events) {
  1599. short lost_event;
  1600. queue->dropped_event = 1;
  1601. retval = pop_gpib_event_nolock(board, queue, &lost_event);
  1602. if (retval < 0)
  1603. return retval;
  1604. }
  1605. event = kmalloc_obj(struct gpib_event, GFP_ATOMIC);
  1606. if (!event) {
  1607. queue->dropped_event = 1;
  1608. dev_err(board->gpib_dev, "failed to allocate memory for event\n");
  1609. return -ENOMEM;
  1610. }
  1611. INIT_LIST_HEAD(&event->list);
  1612. event->event_type = event_type;
  1613. list_add_tail(&event->list, head);
  1614. queue->num_events++;
  1615. dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n",
  1616. (int)event_type, num_gpib_events(queue));
  1617. return 0;
  1618. }
  1619. // push event onto back of event queue
  1620. int push_gpib_event(struct gpib_board *board, short event_type)
  1621. {
  1622. unsigned long flags;
  1623. int retval;
  1624. spin_lock_irqsave(&board->event_queue.lock, flags);
  1625. retval = push_gpib_event_nolock(board, event_type);
  1626. spin_unlock_irqrestore(&board->event_queue.lock, flags);
  1627. if (event_type == EVENT_DEV_TRG)
  1628. board->status |= DTAS;
  1629. if (event_type == EVENT_DEV_CLR)
  1630. board->status |= DCAS;
  1631. return retval;
  1632. }
  1633. EXPORT_SYMBOL(push_gpib_event);
  1634. static int pop_gpib_event_nolock(struct gpib_board *board,
  1635. struct gpib_event_queue *queue, short *event_type)
  1636. {
  1637. struct list_head *head = &queue->event_head;
  1638. struct list_head *front = head->next;
  1639. struct gpib_event *event;
  1640. if (num_gpib_events(queue) == 0) {
  1641. *event_type = EVENT_NONE;
  1642. return 0;
  1643. }
  1644. if (front == head)
  1645. return -EIO;
  1646. if (queue->dropped_event) {
  1647. queue->dropped_event = 0;
  1648. return -EPIPE;
  1649. }
  1650. event = list_entry(front, struct gpib_event, list);
  1651. *event_type = event->event_type;
  1652. list_del(front);
  1653. kfree(event);
  1654. queue->num_events--;
  1655. dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n",
  1656. (int)*event_type, num_gpib_events(queue));
  1657. return 0;
  1658. }
  1659. // pop event from front of event queue
  1660. int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type)
  1661. {
  1662. unsigned long flags;
  1663. int retval;
  1664. spin_lock_irqsave(&queue->lock, flags);
  1665. retval = pop_gpib_event_nolock(board, queue, event_type);
  1666. spin_unlock_irqrestore(&queue->lock, flags);
  1667. return retval;
  1668. }
  1669. static int event_ioctl(struct gpib_board *board, unsigned long arg)
  1670. {
  1671. short user_event;
  1672. int retval;
  1673. short event;
  1674. retval = pop_gpib_event(board, &board->event_queue, &event);
  1675. if (retval < 0)
  1676. return retval;
  1677. user_event = event;
  1678. retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event));
  1679. if (retval)
  1680. return -EFAULT;
  1681. return 0;
  1682. }
  1683. static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg)
  1684. {
  1685. int request_control;
  1686. int retval;
  1687. retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control));
  1688. if (retval)
  1689. return -EFAULT;
  1690. return ibrsc(board, request_control);
  1691. }
  1692. static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg)
  1693. {
  1694. unsigned int cmd;
  1695. unsigned int delay;
  1696. int retval;
  1697. if (!board->interface->t1_delay)
  1698. return -ENOENT;
  1699. retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd));
  1700. if (retval)
  1701. return -EFAULT;
  1702. delay = cmd;
  1703. retval = board->interface->t1_delay(board, delay);
  1704. if (retval < 0)
  1705. return retval;
  1706. board->t1_nano_sec = retval;
  1707. return 0;
  1708. }
  1709. static const struct file_operations ib_fops = {
  1710. .owner = THIS_MODULE,
  1711. .llseek = NULL,
  1712. .unlocked_ioctl = &ibioctl,
  1713. .compat_ioctl = &ibioctl,
  1714. .open = &ibopen,
  1715. .release = &ibclose,
  1716. };
  1717. struct gpib_board board_array[GPIB_MAX_NUM_BOARDS];
  1718. LIST_HEAD(registered_drivers);
  1719. void init_gpib_descriptor(struct gpib_descriptor *desc)
  1720. {
  1721. desc->pad = 0;
  1722. desc->sad = -1;
  1723. desc->is_board = 0;
  1724. desc->autopoll_enabled = 0;
  1725. atomic_set(&desc->io_in_progress, 0);
  1726. atomic_set(&desc->descriptor_busy, 0);
  1727. }
  1728. int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module)
  1729. {
  1730. struct gpib_interface_list *entry;
  1731. entry = kmalloc_obj(*entry);
  1732. if (!entry)
  1733. return -ENOMEM;
  1734. entry->interface = interface;
  1735. entry->module = provider_module;
  1736. list_add(&entry->list, &registered_drivers);
  1737. return 0;
  1738. }
  1739. EXPORT_SYMBOL(gpib_register_driver);
  1740. void gpib_unregister_driver(struct gpib_interface *interface)
  1741. {
  1742. int i;
  1743. struct list_head *list_ptr;
  1744. for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) {
  1745. struct gpib_board *board = &board_array[i];
  1746. if (board->interface == interface) {
  1747. if (board->use_count > 0)
  1748. pr_warn("gpib: Warning: deregistered interface %s in use\n",
  1749. interface->name);
  1750. iboffline(board);
  1751. board->interface = NULL;
  1752. }
  1753. }
  1754. for (list_ptr = registered_drivers.next; list_ptr != &registered_drivers;) {
  1755. struct gpib_interface_list *entry;
  1756. entry = list_entry(list_ptr, struct gpib_interface_list, list);
  1757. list_ptr = list_ptr->next;
  1758. if (entry->interface == interface) {
  1759. list_del(&entry->list);
  1760. kfree(entry);
  1761. }
  1762. }
  1763. }
  1764. EXPORT_SYMBOL(gpib_unregister_driver);
  1765. static void init_gpib_board_config(struct gpib_board_config *config)
  1766. {
  1767. memset(config, 0, sizeof(struct gpib_board_config));
  1768. config->pci_bus = -1;
  1769. config->pci_slot = -1;
  1770. }
  1771. void init_gpib_board(struct gpib_board *board)
  1772. {
  1773. board->interface = NULL;
  1774. board->provider_module = NULL;
  1775. board->buffer = NULL;
  1776. board->buffer_length = 0;
  1777. board->status = 0;
  1778. init_waitqueue_head(&board->wait);
  1779. mutex_init(&board->user_mutex);
  1780. mutex_init(&board->big_gpib_mutex);
  1781. board->locking_pid = 0;
  1782. spin_lock_init(&board->locking_pid_spinlock);
  1783. spin_lock_init(&board->spinlock);
  1784. timer_setup(&board->timer, NULL, 0);
  1785. board->dev = NULL;
  1786. board->gpib_dev = NULL;
  1787. init_gpib_board_config(&board->config);
  1788. board->private_data = NULL;
  1789. board->use_count = 0;
  1790. INIT_LIST_HEAD(&board->device_list);
  1791. board->pad = 0;
  1792. board->sad = -1;
  1793. board->usec_timeout = 3000000;
  1794. board->parallel_poll_configuration = 0;
  1795. board->online = 0;
  1796. board->autospollers = 0;
  1797. board->autospoll_task = NULL;
  1798. init_event_queue(&board->event_queue);
  1799. board->minor = -1;
  1800. init_gpib_pseudo_irq(&board->pseudo_irq);
  1801. board->master = 1;
  1802. atomic_set(&board->stuck_srq, 0);
  1803. board->local_ppoll_mode = 0;
  1804. }
  1805. int gpib_allocate_board(struct gpib_board *board)
  1806. {
  1807. if (!board->buffer) {
  1808. board->buffer_length = 0x4000;
  1809. board->buffer = vmalloc(board->buffer_length);
  1810. if (!board->buffer) {
  1811. board->buffer_length = 0;
  1812. return -ENOMEM;
  1813. }
  1814. }
  1815. return 0;
  1816. }
  1817. void gpib_deallocate_board(struct gpib_board *board)
  1818. {
  1819. short dummy;
  1820. if (board->buffer) {
  1821. vfree(board->buffer);
  1822. board->buffer = NULL;
  1823. board->buffer_length = 0;
  1824. }
  1825. while (num_gpib_events(&board->event_queue))
  1826. pop_gpib_event(board, &board->event_queue, &dummy);
  1827. }
  1828. static void init_board_array(struct gpib_board *board_array, unsigned int length)
  1829. {
  1830. int i;
  1831. for (i = 0; i < length; i++) {
  1832. init_gpib_board(&board_array[i]);
  1833. board_array[i].minor = i;
  1834. }
  1835. }
  1836. void init_gpib_status_queue(struct gpib_status_queue *device)
  1837. {
  1838. INIT_LIST_HEAD(&device->list);
  1839. INIT_LIST_HEAD(&device->status_bytes);
  1840. device->num_status_bytes = 0;
  1841. device->reference_count = 0;
  1842. device->dropped_byte = 0;
  1843. }
  1844. static struct class *gpib_class;
  1845. static int __init gpib_common_init_module(void)
  1846. {
  1847. int i;
  1848. pr_info("GPIB core driver\n");
  1849. init_board_array(board_array, GPIB_MAX_NUM_BOARDS);
  1850. if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) {
  1851. pr_err("gpib: can't get major %d\n", GPIB_CODE);
  1852. return -EIO;
  1853. }
  1854. gpib_class = class_create("gpib_common");
  1855. if (IS_ERR(gpib_class)) {
  1856. pr_err("gpib: failed to create gpib class\n");
  1857. unregister_chrdev(GPIB_CODE, "gpib");
  1858. return PTR_ERR(gpib_class);
  1859. }
  1860. for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i)
  1861. board_array[i].gpib_dev = device_create(gpib_class, NULL,
  1862. MKDEV(GPIB_CODE, i), NULL, "gpib%i", i);
  1863. return 0;
  1864. }
  1865. static void __exit gpib_common_exit_module(void)
  1866. {
  1867. int i;
  1868. for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i)
  1869. device_destroy(gpib_class, MKDEV(GPIB_CODE, i));
  1870. class_destroy(gpib_class);
  1871. unregister_chrdev(GPIB_CODE, "gpib");
  1872. }
  1873. int gpib_match_device_path(struct device *dev, const char *device_path_in)
  1874. {
  1875. if (device_path_in) {
  1876. char *device_path;
  1877. device_path = kobject_get_path(&dev->kobj, GFP_KERNEL);
  1878. if (!device_path) {
  1879. dev_err(dev, "kobject_get_path returned NULL.");
  1880. return 0;
  1881. }
  1882. if (strcmp(device_path_in, device_path) != 0) {
  1883. kfree(device_path);
  1884. return 0;
  1885. }
  1886. kfree(device_path);
  1887. }
  1888. return 1;
  1889. }
  1890. EXPORT_SYMBOL(gpib_match_device_path);
  1891. struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id,
  1892. unsigned int device_id, struct pci_dev *from)
  1893. {
  1894. struct pci_dev *pci_device = from;
  1895. while ((pci_device = pci_get_device(vendor_id, device_id, pci_device))) {
  1896. if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number)
  1897. continue;
  1898. if (config->pci_slot >= 0 && config->pci_slot !=
  1899. PCI_SLOT(pci_device->devfn))
  1900. continue;
  1901. if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0)
  1902. continue;
  1903. return pci_device;
  1904. }
  1905. return NULL;
  1906. }
  1907. EXPORT_SYMBOL(gpib_pci_get_device);
  1908. struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id,
  1909. unsigned int device_id, unsigned int ss_vendor,
  1910. unsigned int ss_device,
  1911. struct pci_dev *from)
  1912. {
  1913. struct pci_dev *pci_device = from;
  1914. while ((pci_device = pci_get_subsys(vendor_id, device_id,
  1915. ss_vendor, ss_device, pci_device))) {
  1916. if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number)
  1917. continue;
  1918. if (config->pci_slot >= 0 && config->pci_slot !=
  1919. PCI_SLOT(pci_device->devfn))
  1920. continue;
  1921. if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0)
  1922. continue;
  1923. return pci_device;
  1924. }
  1925. return NULL;
  1926. }
  1927. EXPORT_SYMBOL(gpib_pci_get_subsys);
  1928. module_init(gpib_common_init_module);
  1929. module_exit(gpib_common_exit_module);