iblib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // SPDX-License-Identifier: GPL-2.0
  2. /***************************************************************************
  3. * copyright : (C) 2001, 2002 by Frank Mori Hess
  4. ***************************************************************************/
  5. #define dev_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include "ibsys.h"
  7. #include <linux/delay.h>
  8. #include <linux/kthread.h>
  9. #include <linux/vmalloc.h>
  10. /*
  11. * IBCAC
  12. * Return to the controller active state from the
  13. * controller standby state, i.e., turn ATN on. Note
  14. * that in order to enter the controller active state
  15. * from the controller idle state, ibsic must be called.
  16. * If sync is non-zero, attempt to take control synchronously.
  17. * If fallback_to_async is non-zero, try to take control asynchronously
  18. * if synchronous attempt fails.
  19. */
  20. int ibcac(struct gpib_board *board, int sync, int fallback_to_async)
  21. {
  22. int status = ibstatus(board);
  23. int retval;
  24. if ((status & CIC) == 0)
  25. return -EINVAL;
  26. if (status & ATN)
  27. return 0;
  28. if (sync && (status & LACS) == 0)
  29. /*
  30. * tcs (take control synchronously) can only possibly work when
  31. * controller is listener. Error code also needs to be -ETIMEDOUT
  32. * or it will giveout without doing fallback.
  33. */
  34. retval = -ETIMEDOUT;
  35. else
  36. retval = board->interface->take_control(board, sync);
  37. if (retval < 0 && fallback_to_async) {
  38. if (sync && retval == -ETIMEDOUT)
  39. retval = board->interface->take_control(board, 0);
  40. }
  41. board->interface->update_status(board, 0);
  42. return retval;
  43. }
  44. /*
  45. * After ATN is asserted, it should cause any connected devices
  46. * to start listening for command bytes and leave acceptor idle state.
  47. * So if ATN is asserted and neither NDAC or NRFD are asserted,
  48. * then there are no devices and ibcmd should error out immediately.
  49. * Some gpib hardware sees itself asserting NDAC/NRFD when it
  50. * is controller in charge, in which case this check will
  51. * do nothing useful (but shouldn't cause any harm either).
  52. * Drivers that don't need this check (ni_usb for example) may
  53. * set the skip_check_for_command_acceptors flag in their
  54. * gpib_interface_struct to avoid useless overhead.
  55. */
  56. static int check_for_command_acceptors(struct gpib_board *board)
  57. {
  58. int lines;
  59. if (board->interface->skip_check_for_command_acceptors)
  60. return 0;
  61. if (!board->interface->line_status)
  62. return 0;
  63. udelay(2); // allow time for devices to respond to ATN if it was just asserted
  64. lines = board->interface->line_status(board);
  65. if (lines < 0)
  66. return lines;
  67. if ((lines & VALID_NRFD) && (lines & VALID_NDAC)) {
  68. if ((lines & BUS_NRFD) == 0 && (lines & BUS_NDAC) == 0)
  69. return -ENOTCONN;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * IBCMD
  75. * Write cnt command bytes from buf to the GPIB. The
  76. * command operation terminates only on I/O complete.
  77. *
  78. * NOTE:
  79. * 1. Prior to beginning the command, the interface is
  80. * placed in the controller active state.
  81. * 2. Before calling ibcmd for the first time, ibsic
  82. * must be called to initialize the GPIB and enable
  83. * the interface to leave the controller idle state.
  84. */
  85. int ibcmd(struct gpib_board *board, u8 *buf, size_t length, size_t *bytes_written)
  86. {
  87. ssize_t ret = 0;
  88. int status;
  89. *bytes_written = 0;
  90. status = ibstatus(board);
  91. if ((status & CIC) == 0)
  92. return -EINVAL;
  93. os_start_timer(board, board->usec_timeout);
  94. ret = ibcac(board, 1, 1);
  95. if (ret == 0) {
  96. ret = check_for_command_acceptors(board);
  97. if (ret == 0)
  98. ret = board->interface->command(board, buf, length, bytes_written);
  99. }
  100. os_remove_timer(board);
  101. if (io_timed_out(board))
  102. ret = -ETIMEDOUT;
  103. return ret;
  104. }
  105. /*
  106. * IBGTS
  107. * Go to the controller standby state from the controller
  108. * active state, i.e., turn ATN off.
  109. */
  110. int ibgts(struct gpib_board *board)
  111. {
  112. int status = ibstatus(board);
  113. int retval;
  114. if ((status & CIC) == 0)
  115. return -EINVAL;
  116. retval = board->interface->go_to_standby(board); /* go to standby */
  117. board->interface->update_status(board, 0);
  118. return retval;
  119. }
  120. static int autospoll_wait_should_wake_up(struct gpib_board *board)
  121. {
  122. int retval;
  123. mutex_lock(&board->big_gpib_mutex);
  124. retval = board->master && board->autospollers > 0 &&
  125. !atomic_read(&board->stuck_srq) &&
  126. test_and_clear_bit(SRQI_NUM, &board->status);
  127. mutex_unlock(&board->big_gpib_mutex);
  128. return retval;
  129. }
  130. static int autospoll_thread(void *board_void)
  131. {
  132. struct gpib_board *board = board_void;
  133. int retval = 0;
  134. dev_dbg(board->gpib_dev, "entering autospoll thread\n");
  135. while (1) {
  136. wait_event_interruptible(board->wait,
  137. kthread_should_stop() ||
  138. autospoll_wait_should_wake_up(board));
  139. dev_dbg(board->gpib_dev, "autospoll wait satisfied\n");
  140. if (kthread_should_stop())
  141. break;
  142. mutex_lock(&board->big_gpib_mutex);
  143. /* make sure we are still good after we have lock */
  144. if (board->autospollers <= 0 || board->master == 0) {
  145. mutex_unlock(&board->big_gpib_mutex);
  146. continue;
  147. }
  148. mutex_unlock(&board->big_gpib_mutex);
  149. if (try_module_get(board->provider_module)) {
  150. retval = autopoll_all_devices(board);
  151. module_put(board->provider_module);
  152. } else {
  153. dev_err(board->gpib_dev, "try_module_get() failed!\n");
  154. }
  155. if (retval <= 0) {
  156. dev_err(board->gpib_dev, "stuck SRQ\n");
  157. atomic_set(&board->stuck_srq, 1); // XXX could be better
  158. set_bit(SRQI_NUM, &board->status);
  159. }
  160. }
  161. return retval;
  162. }
  163. int ibonline(struct gpib_board *board)
  164. {
  165. int retval;
  166. if (board->online)
  167. return -EBUSY;
  168. if (!board->interface)
  169. return -ENODEV;
  170. retval = gpib_allocate_board(board);
  171. if (retval < 0)
  172. return retval;
  173. board->dev = NULL;
  174. board->local_ppoll_mode = 0;
  175. retval = board->interface->attach(board, &board->config);
  176. if (retval < 0) {
  177. board->interface->detach(board);
  178. return retval;
  179. }
  180. /*
  181. * nios2nommu on 2.6.11 uclinux kernel has weird problems
  182. * with autospoll thread causing huge slowdowns
  183. */
  184. #ifndef CONFIG_NIOS2
  185. board->autospoll_task = kthread_run(&autospoll_thread, board,
  186. "gpib%d_autospoll_kthread", board->minor);
  187. if (IS_ERR(board->autospoll_task)) {
  188. dev_err(board->gpib_dev, "failed to create autospoll thread\n");
  189. board->interface->detach(board);
  190. return PTR_ERR(board->autospoll_task);
  191. }
  192. #endif
  193. board->online = 1;
  194. dev_dbg(board->gpib_dev, "board online\n");
  195. return 0;
  196. }
  197. /* XXX need to make sure board is generally not in use (grab board lock?) */
  198. int iboffline(struct gpib_board *board)
  199. {
  200. int retval;
  201. if (board->online == 0)
  202. return 0;
  203. if (!board->interface)
  204. return -ENODEV;
  205. if (board->autospoll_task && !IS_ERR(board->autospoll_task)) {
  206. retval = kthread_stop(board->autospoll_task);
  207. if (retval)
  208. dev_err(board->gpib_dev, "kthread_stop returned %i\n", retval);
  209. board->autospoll_task = NULL;
  210. }
  211. board->interface->detach(board);
  212. gpib_deallocate_board(board);
  213. board->online = 0;
  214. dev_dbg(board->gpib_dev, "board offline\n");
  215. return 0;
  216. }
  217. /*
  218. * IBLINES
  219. * Poll the GPIB control lines and return their status in buf.
  220. *
  221. * LSB (bits 0-7) - VALID lines mask (lines that can be monitored).
  222. * Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set).
  223. *
  224. */
  225. int iblines(const struct gpib_board *board, short *lines)
  226. {
  227. int retval;
  228. *lines = 0;
  229. if (!board->interface->line_status)
  230. return 0;
  231. retval = board->interface->line_status(board);
  232. if (retval < 0)
  233. return retval;
  234. *lines = retval;
  235. return 0;
  236. }
  237. /*
  238. * IBRD
  239. * Read up to 'length' bytes of data from the GPIB into buf. End
  240. * on detection of END (EOI and or EOS) and set 'end_flag'.
  241. *
  242. * NOTE:
  243. * 1. The interface is placed in the controller standby
  244. * state prior to beginning the read.
  245. * 2. Prior to calling ibrd, the intended devices as well
  246. * as the interface board itself must be addressed by
  247. * calling ibcmd.
  248. */
  249. int ibrd(struct gpib_board *board, u8 *buf, size_t length, int *end_flag, size_t *nbytes)
  250. {
  251. ssize_t ret = 0;
  252. int retval;
  253. size_t bytes_read;
  254. *nbytes = 0;
  255. *end_flag = 0;
  256. if (length == 0)
  257. return 0;
  258. if (board->master) {
  259. retval = ibgts(board);
  260. if (retval < 0)
  261. return retval;
  262. }
  263. /*
  264. * XXX resetting timer here could cause timeouts take longer than they should,
  265. * since read_ioctl calls this
  266. * function in a loop, there is probably a similar problem with writes/commands
  267. */
  268. os_start_timer(board, board->usec_timeout);
  269. do {
  270. ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read);
  271. if (ret < 0)
  272. goto ibrd_out;
  273. buf += bytes_read;
  274. *nbytes += bytes_read;
  275. if (need_resched())
  276. schedule();
  277. } while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0);
  278. ibrd_out:
  279. os_remove_timer(board);
  280. return ret;
  281. }
  282. /*
  283. * IBRPP
  284. * Conduct a parallel poll and return the byte in buf.
  285. *
  286. * NOTE:
  287. * 1. Prior to conducting the poll the interface is placed
  288. * in the controller active state.
  289. */
  290. int ibrpp(struct gpib_board *board, u8 *result)
  291. {
  292. int retval = 0;
  293. os_start_timer(board, board->usec_timeout);
  294. retval = ibcac(board, 1, 1);
  295. if (retval)
  296. return -1;
  297. retval = board->interface->parallel_poll(board, result);
  298. os_remove_timer(board);
  299. return retval;
  300. }
  301. int ibppc(struct gpib_board *board, u8 configuration)
  302. {
  303. configuration &= 0x1f;
  304. board->interface->parallel_poll_configure(board, configuration);
  305. board->parallel_poll_configuration = configuration;
  306. return 0;
  307. }
  308. int ibrsv2(struct gpib_board *board, u8 status_byte, int new_reason_for_service)
  309. {
  310. int board_status = ibstatus(board);
  311. const unsigned int MSS = status_byte & request_service_bit;
  312. if ((board_status & CIC))
  313. return -EINVAL;
  314. if (MSS == 0 && new_reason_for_service)
  315. return -EINVAL;
  316. if (board->interface->serial_poll_response2) {
  317. board->interface->serial_poll_response2(board, status_byte, new_reason_for_service);
  318. // fall back on simpler serial_poll_response if the behavior would be the same
  319. } else if (board->interface->serial_poll_response &&
  320. (MSS == 0 || (MSS && new_reason_for_service))) {
  321. board->interface->serial_poll_response(board, status_byte);
  322. } else {
  323. return -EOPNOTSUPP;
  324. }
  325. return 0;
  326. }
  327. /*
  328. * IBSIC
  329. * Send IFC for at least 100 microseconds.
  330. *
  331. * NOTE:
  332. * 1. Ibsic must be called prior to the first call to
  333. * ibcmd in order to initialize the bus and enable the
  334. * interface to leave the controller idle state.
  335. */
  336. int ibsic(struct gpib_board *board, unsigned int usec_duration)
  337. {
  338. if (board->master == 0)
  339. return -EINVAL;
  340. if (usec_duration < 100)
  341. usec_duration = 100;
  342. if (usec_duration > 1000)
  343. usec_duration = 1000;
  344. dev_dbg(board->gpib_dev, "sending interface clear, delay = %ius\n", usec_duration);
  345. board->interface->interface_clear(board, 1);
  346. udelay(usec_duration);
  347. board->interface->interface_clear(board, 0);
  348. return 0;
  349. }
  350. int ibrsc(struct gpib_board *board, int request_control)
  351. {
  352. int retval;
  353. if (!board->interface->request_system_control)
  354. return -EPERM;
  355. retval = board->interface->request_system_control(board, request_control);
  356. if (retval)
  357. return retval;
  358. board->master = request_control != 0;
  359. return 0;
  360. }
  361. /*
  362. * IBSRE
  363. * Send REN true if v is non-zero or false if v is zero.
  364. */
  365. int ibsre(struct gpib_board *board, int enable)
  366. {
  367. if (board->master == 0)
  368. return -EINVAL;
  369. board->interface->remote_enable(board, enable); /* set or clear REN */
  370. if (!enable)
  371. usleep_range(100, 150);
  372. return 0;
  373. }
  374. /*
  375. * IBPAD
  376. * change the GPIB address of the interface board. The address
  377. * must be 0 through 30. ibonl resets the address to PAD.
  378. */
  379. int ibpad(struct gpib_board *board, unsigned int addr)
  380. {
  381. if (addr > MAX_GPIB_PRIMARY_ADDRESS)
  382. return -EINVAL;
  383. board->pad = addr;
  384. if (board->online)
  385. board->interface->primary_address(board, board->pad);
  386. dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad);
  387. return 0;
  388. }
  389. /*
  390. * IBSAD
  391. * change the secondary GPIB address of the interface board.
  392. * The address must be 0 through 30, or negative disables. ibonl resets the
  393. * address to SAD.
  394. */
  395. int ibsad(struct gpib_board *board, int addr)
  396. {
  397. if (addr > MAX_GPIB_SECONDARY_ADDRESS)
  398. return -EINVAL;
  399. board->sad = addr;
  400. if (board->online) {
  401. if (board->sad >= 0)
  402. board->interface->secondary_address(board, board->sad, 1);
  403. else
  404. board->interface->secondary_address(board, 0, 0);
  405. }
  406. dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad);
  407. return 0;
  408. }
  409. /*
  410. * IBEOS
  411. * Set the end-of-string modes for I/O operations to v.
  412. *
  413. */
  414. int ibeos(struct gpib_board *board, int eos, int eosflags)
  415. {
  416. int retval;
  417. if (eosflags & ~EOS_MASK)
  418. return -EINVAL;
  419. if (eosflags & REOS) {
  420. retval = board->interface->enable_eos(board, eos, eosflags & BIN);
  421. } else {
  422. board->interface->disable_eos(board);
  423. retval = 0;
  424. }
  425. return retval;
  426. }
  427. int ibstatus(struct gpib_board *board)
  428. {
  429. return general_ibstatus(board, NULL, 0, 0, NULL);
  430. }
  431. int general_ibstatus(struct gpib_board *board, const struct gpib_status_queue *device,
  432. int clear_mask, int set_mask, struct gpib_descriptor *desc)
  433. {
  434. int status = 0;
  435. short line_status;
  436. if (board->private_data) {
  437. status = board->interface->update_status(board, clear_mask);
  438. /*
  439. * XXX should probably stop having drivers use TIMO bit in
  440. * board->status to avoid confusion
  441. */
  442. status &= ~TIMO;
  443. /* get real SRQI status if we can */
  444. if (iblines(board, &line_status) == 0) {
  445. if ((line_status & VALID_SRQ)) {
  446. if ((line_status & BUS_SRQ))
  447. status |= SRQI;
  448. else
  449. status &= ~SRQI;
  450. }
  451. }
  452. }
  453. if (device)
  454. if (num_status_bytes(device))
  455. status |= RQS;
  456. if (desc) {
  457. if (set_mask & CMPL)
  458. atomic_set(&desc->io_in_progress, 0);
  459. else if (clear_mask & CMPL)
  460. atomic_set(&desc->io_in_progress, 1);
  461. if (atomic_read(&desc->io_in_progress))
  462. status &= ~CMPL;
  463. else
  464. status |= CMPL;
  465. }
  466. if (num_gpib_events(&board->event_queue))
  467. status |= EVENT;
  468. else
  469. status &= ~EVENT;
  470. return status;
  471. }
  472. struct wait_info {
  473. struct gpib_board *board;
  474. struct timer_list timer;
  475. int timed_out;
  476. unsigned long usec_timeout;
  477. };
  478. static void wait_timeout(struct timer_list *t)
  479. {
  480. struct wait_info *winfo = timer_container_of(winfo, t, timer);
  481. winfo->timed_out = 1;
  482. wake_up_interruptible(&winfo->board->wait);
  483. }
  484. static void init_wait_info(struct wait_info *winfo)
  485. {
  486. winfo->board = NULL;
  487. winfo->timed_out = 0;
  488. timer_setup_on_stack(&winfo->timer, wait_timeout, 0);
  489. }
  490. static int wait_satisfied(struct wait_info *winfo, struct gpib_status_queue *status_queue,
  491. int wait_mask, int *status, struct gpib_descriptor *desc)
  492. {
  493. struct gpib_board *board = winfo->board;
  494. int temp_status;
  495. if (mutex_lock_interruptible(&board->big_gpib_mutex))
  496. return -ERESTARTSYS;
  497. temp_status = general_ibstatus(board, status_queue, 0, 0, desc);
  498. mutex_unlock(&board->big_gpib_mutex);
  499. if (winfo->timed_out)
  500. temp_status |= TIMO;
  501. else
  502. temp_status &= ~TIMO;
  503. if (wait_mask & temp_status) {
  504. *status = temp_status;
  505. return 1;
  506. }
  507. // XXX does wait for END work?
  508. return 0;
  509. }
  510. /* install timer interrupt handler */
  511. static void start_wait_timer(struct wait_info *winfo)
  512. /* Starts the timeout task */
  513. {
  514. winfo->timed_out = 0;
  515. if (winfo->usec_timeout > 0)
  516. mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout));
  517. }
  518. static void remove_wait_timer(struct wait_info *winfo)
  519. {
  520. timer_delete_sync(&winfo->timer);
  521. timer_destroy_on_stack(&winfo->timer);
  522. }
  523. /*
  524. * IBWAIT
  525. * Check or wait for a GPIB event to occur. The mask argument
  526. * is a bit vector corresponding to the status bit vector. It
  527. * has a bit set for each condition which can terminate the wait
  528. * If the mask is 0 then
  529. * no condition is waited for.
  530. */
  531. int ibwait(struct gpib_board *board, int wait_mask, int clear_mask, int set_mask,
  532. int *status, unsigned long usec_timeout, struct gpib_descriptor *desc)
  533. {
  534. int retval = 0;
  535. struct gpib_status_queue *status_queue;
  536. struct wait_info winfo;
  537. if (desc->is_board)
  538. status_queue = NULL;
  539. else
  540. status_queue = get_gpib_status_queue(board, desc->pad, desc->sad);
  541. if (wait_mask == 0) {
  542. *status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc);
  543. return 0;
  544. }
  545. mutex_unlock(&board->big_gpib_mutex);
  546. init_wait_info(&winfo);
  547. winfo.board = board;
  548. winfo.usec_timeout = usec_timeout;
  549. start_wait_timer(&winfo);
  550. if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue,
  551. wait_mask, status, desc))) {
  552. dev_dbg(board->gpib_dev, "wait interrupted\n");
  553. retval = -ERESTARTSYS;
  554. }
  555. remove_wait_timer(&winfo);
  556. if (retval)
  557. return retval;
  558. if (mutex_lock_interruptible(&board->big_gpib_mutex))
  559. return -ERESTARTSYS;
  560. /* make sure we only clear status bits that we are reporting */
  561. if (*status & clear_mask || set_mask)
  562. general_ibstatus(board, status_queue, *status & clear_mask, set_mask, NULL);
  563. return 0;
  564. }
  565. /*
  566. * IBWRT
  567. * Write cnt bytes of data from buf to the GPIB. The write
  568. * operation terminates only on I/O complete.
  569. *
  570. * NOTE:
  571. * 1. Prior to beginning the write, the interface is
  572. * placed in the controller standby state.
  573. * 2. Prior to calling ibwrt, the intended devices as
  574. * well as the interface board itself must be
  575. * addressed by calling ibcmd.
  576. */
  577. int ibwrt(struct gpib_board *board, u8 *buf, size_t cnt, int send_eoi, size_t *bytes_written)
  578. {
  579. int ret = 0;
  580. int retval;
  581. if (cnt == 0)
  582. return 0;
  583. if (board->master) {
  584. retval = ibgts(board);
  585. if (retval < 0)
  586. return retval;
  587. }
  588. os_start_timer(board, board->usec_timeout);
  589. ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written);
  590. if (io_timed_out(board))
  591. ret = -ETIMEDOUT;
  592. os_remove_timer(board);
  593. return ret;
  594. }