ircomm_tty_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*********************************************************************
  2. *
  3. * Filename: ircomm_tty_ioctl.c
  4. * Version:
  5. * Description:
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Thu Jun 10 14:39:09 1999
  9. * Modified at: Wed Jan 5 14:45:43 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. *
  27. ********************************************************************/
  28. #include <linux/init.h>
  29. #include <linux/fs.h>
  30. #include <linux/termios.h>
  31. #include <linux/tty.h>
  32. #include <linux/serial.h>
  33. #include <asm/uaccess.h>
  34. #include <net/irda/irda.h>
  35. #include <net/irda/irmod.h>
  36. #include <net/irda/ircomm_core.h>
  37. #include <net/irda/ircomm_param.h>
  38. #include <net/irda/ircomm_tty_attach.h>
  39. #include <net/irda/ircomm_tty.h>
  40. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  41. /*
  42. * Function ircomm_tty_change_speed (driver)
  43. *
  44. * Change speed of the driver. If the remote device is a DCE, then this
  45. * should make it change the speed of its serial port
  46. */
  47. static void ircomm_tty_change_speed(struct ircomm_tty_cb *self,
  48. struct tty_struct *tty)
  49. {
  50. unsigned int cflag, cval;
  51. int baud;
  52. if (!self->ircomm)
  53. return;
  54. cflag = tty->termios.c_cflag;
  55. /* byte size and parity */
  56. switch (cflag & CSIZE) {
  57. case CS5: cval = IRCOMM_WSIZE_5; break;
  58. case CS6: cval = IRCOMM_WSIZE_6; break;
  59. case CS7: cval = IRCOMM_WSIZE_7; break;
  60. case CS8: cval = IRCOMM_WSIZE_8; break;
  61. default: cval = IRCOMM_WSIZE_5; break;
  62. }
  63. if (cflag & CSTOPB)
  64. cval |= IRCOMM_2_STOP_BIT;
  65. if (cflag & PARENB)
  66. cval |= IRCOMM_PARITY_ENABLE;
  67. if (!(cflag & PARODD))
  68. cval |= IRCOMM_PARITY_EVEN;
  69. /* Determine divisor based on baud rate */
  70. baud = tty_get_baud_rate(tty);
  71. if (!baud)
  72. baud = 9600; /* B0 transition handled in rs_set_termios */
  73. self->settings.data_rate = baud;
  74. ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
  75. /* CTS flow control flag and modem status interrupts */
  76. if (cflag & CRTSCTS) {
  77. self->port.flags |= ASYNC_CTS_FLOW;
  78. self->settings.flow_control |= IRCOMM_RTS_CTS_IN;
  79. /* This got me. Bummer. Jean II */
  80. if (self->service_type == IRCOMM_3_WIRE_RAW)
  81. net_warn_ratelimited("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n",
  82. __func__);
  83. } else {
  84. self->port.flags &= ~ASYNC_CTS_FLOW;
  85. self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
  86. }
  87. if (cflag & CLOCAL)
  88. self->port.flags &= ~ASYNC_CHECK_CD;
  89. else
  90. self->port.flags |= ASYNC_CHECK_CD;
  91. #if 0
  92. /*
  93. * Set up parity check flag
  94. */
  95. if (I_INPCK(self->tty))
  96. driver->read_status_mask |= LSR_FE | LSR_PE;
  97. if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
  98. driver->read_status_mask |= LSR_BI;
  99. /*
  100. * Characters to ignore
  101. */
  102. driver->ignore_status_mask = 0;
  103. if (I_IGNPAR(driver->tty))
  104. driver->ignore_status_mask |= LSR_PE | LSR_FE;
  105. if (I_IGNBRK(self->tty)) {
  106. self->ignore_status_mask |= LSR_BI;
  107. /*
  108. * If we're ignore parity and break indicators, ignore
  109. * overruns too. (For real raw support).
  110. */
  111. if (I_IGNPAR(self->tty))
  112. self->ignore_status_mask |= LSR_OE;
  113. }
  114. #endif
  115. self->settings.data_format = cval;
  116. ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
  117. ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
  118. }
  119. /*
  120. * Function ircomm_tty_set_termios (tty, old_termios)
  121. *
  122. * This routine allows the tty driver to be notified when device's
  123. * termios settings have changed. Note that a well-designed tty driver
  124. * should be prepared to accept the case where old == NULL, and try to
  125. * do something rational.
  126. */
  127. void ircomm_tty_set_termios(struct tty_struct *tty,
  128. struct ktermios *old_termios)
  129. {
  130. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  131. unsigned int cflag = tty->termios.c_cflag;
  132. if ((cflag == old_termios->c_cflag) &&
  133. (RELEVANT_IFLAG(tty->termios.c_iflag) ==
  134. RELEVANT_IFLAG(old_termios->c_iflag)))
  135. {
  136. return;
  137. }
  138. ircomm_tty_change_speed(self, tty);
  139. /* Handle transition to B0 status */
  140. if ((old_termios->c_cflag & CBAUD) &&
  141. !(cflag & CBAUD)) {
  142. self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
  143. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  144. }
  145. /* Handle transition away from B0 status */
  146. if (!(old_termios->c_cflag & CBAUD) &&
  147. (cflag & CBAUD)) {
  148. self->settings.dte |= IRCOMM_DTR;
  149. if (!(tty->termios.c_cflag & CRTSCTS) ||
  150. !test_bit(TTY_THROTTLED, &tty->flags)) {
  151. self->settings.dte |= IRCOMM_RTS;
  152. }
  153. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  154. }
  155. /* Handle turning off CRTSCTS */
  156. if ((old_termios->c_cflag & CRTSCTS) &&
  157. !(tty->termios.c_cflag & CRTSCTS))
  158. {
  159. tty->hw_stopped = 0;
  160. ircomm_tty_start(tty);
  161. }
  162. }
  163. /*
  164. * Function ircomm_tty_tiocmget (tty)
  165. *
  166. *
  167. *
  168. */
  169. int ircomm_tty_tiocmget(struct tty_struct *tty)
  170. {
  171. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  172. unsigned int result;
  173. if (tty->flags & (1 << TTY_IO_ERROR))
  174. return -EIO;
  175. result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
  176. | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
  177. | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
  178. | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
  179. | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
  180. | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
  181. return result;
  182. }
  183. /*
  184. * Function ircomm_tty_tiocmset (tty, set, clear)
  185. *
  186. *
  187. *
  188. */
  189. int ircomm_tty_tiocmset(struct tty_struct *tty,
  190. unsigned int set, unsigned int clear)
  191. {
  192. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  193. if (tty->flags & (1 << TTY_IO_ERROR))
  194. return -EIO;
  195. IRDA_ASSERT(self != NULL, return -1;);
  196. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  197. if (set & TIOCM_RTS)
  198. self->settings.dte |= IRCOMM_RTS;
  199. if (set & TIOCM_DTR)
  200. self->settings.dte |= IRCOMM_DTR;
  201. if (clear & TIOCM_RTS)
  202. self->settings.dte &= ~IRCOMM_RTS;
  203. if (clear & TIOCM_DTR)
  204. self->settings.dte &= ~IRCOMM_DTR;
  205. if ((set|clear) & TIOCM_RTS)
  206. self->settings.dte |= IRCOMM_DELTA_RTS;
  207. if ((set|clear) & TIOCM_DTR)
  208. self->settings.dte |= IRCOMM_DELTA_DTR;
  209. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  210. return 0;
  211. }
  212. /*
  213. * Function get_serial_info (driver, retinfo)
  214. *
  215. *
  216. *
  217. */
  218. static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
  219. struct serial_struct __user *retinfo)
  220. {
  221. struct serial_struct info;
  222. if (!retinfo)
  223. return -EFAULT;
  224. memset(&info, 0, sizeof(info));
  225. info.line = self->line;
  226. info.flags = self->port.flags;
  227. info.baud_base = self->settings.data_rate;
  228. info.close_delay = self->port.close_delay;
  229. info.closing_wait = self->port.closing_wait;
  230. /* For compatibility */
  231. info.type = PORT_16550A;
  232. info.port = 0;
  233. info.irq = 0;
  234. info.xmit_fifo_size = 0;
  235. info.hub6 = 0;
  236. info.custom_divisor = 0;
  237. if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
  238. return -EFAULT;
  239. return 0;
  240. }
  241. /*
  242. * Function set_serial_info (driver, new_info)
  243. *
  244. *
  245. *
  246. */
  247. static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
  248. struct serial_struct __user *new_info)
  249. {
  250. #if 0
  251. struct serial_struct new_serial;
  252. struct ircomm_tty_cb old_state, *state;
  253. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  254. return -EFAULT;
  255. state = self
  256. old_state = *self;
  257. if (!capable(CAP_SYS_ADMIN)) {
  258. if ((new_serial.baud_base != state->settings.data_rate) ||
  259. (new_serial.close_delay != state->close_delay) ||
  260. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  261. (self->flags & ~ASYNC_USR_MASK)))
  262. return -EPERM;
  263. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  264. (new_serial.flags & ASYNC_USR_MASK));
  265. self->flags = ((self->flags & ~ASYNC_USR_MASK) |
  266. (new_serial.flags & ASYNC_USR_MASK));
  267. /* self->custom_divisor = new_serial.custom_divisor; */
  268. goto check_and_exit;
  269. }
  270. /*
  271. * OK, past this point, all the error checking has been done.
  272. * At this point, we start making changes.....
  273. */
  274. if (self->settings.data_rate != new_serial.baud_base) {
  275. self->settings.data_rate = new_serial.baud_base;
  276. ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
  277. }
  278. self->close_delay = new_serial.close_delay * HZ/100;
  279. self->closing_wait = new_serial.closing_wait * HZ/100;
  280. /* self->custom_divisor = new_serial.custom_divisor; */
  281. self->flags = ((self->flags & ~ASYNC_FLAGS) |
  282. (new_serial.flags & ASYNC_FLAGS));
  283. self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  284. check_and_exit:
  285. if (self->flags & ASYNC_INITIALIZED) {
  286. if (((old_state.flags & ASYNC_SPD_MASK) !=
  287. (self->flags & ASYNC_SPD_MASK)) ||
  288. (old_driver.custom_divisor != driver->custom_divisor)) {
  289. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  290. driver->tty->alt_speed = 57600;
  291. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  292. driver->tty->alt_speed = 115200;
  293. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  294. driver->tty->alt_speed = 230400;
  295. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  296. driver->tty->alt_speed = 460800;
  297. ircomm_tty_change_speed(driver);
  298. }
  299. }
  300. #endif
  301. return 0;
  302. }
  303. /*
  304. * Function ircomm_tty_ioctl (tty, cmd, arg)
  305. *
  306. *
  307. *
  308. */
  309. int ircomm_tty_ioctl(struct tty_struct *tty,
  310. unsigned int cmd, unsigned long arg)
  311. {
  312. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  313. int ret = 0;
  314. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  315. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  316. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  317. if (tty->flags & (1 << TTY_IO_ERROR))
  318. return -EIO;
  319. }
  320. switch (cmd) {
  321. case TIOCGSERIAL:
  322. ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
  323. break;
  324. case TIOCSSERIAL:
  325. ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
  326. break;
  327. case TIOCMIWAIT:
  328. pr_debug("(), TIOCMIWAIT, not impl!\n");
  329. break;
  330. case TIOCGICOUNT:
  331. pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__);
  332. #if 0
  333. save_flags(flags); cli();
  334. cnow = driver->icount;
  335. restore_flags(flags);
  336. p_cuser = (struct serial_icounter_struct __user *) arg;
  337. if (put_user(cnow.cts, &p_cuser->cts) ||
  338. put_user(cnow.dsr, &p_cuser->dsr) ||
  339. put_user(cnow.rng, &p_cuser->rng) ||
  340. put_user(cnow.dcd, &p_cuser->dcd) ||
  341. put_user(cnow.rx, &p_cuser->rx) ||
  342. put_user(cnow.tx, &p_cuser->tx) ||
  343. put_user(cnow.frame, &p_cuser->frame) ||
  344. put_user(cnow.overrun, &p_cuser->overrun) ||
  345. put_user(cnow.parity, &p_cuser->parity) ||
  346. put_user(cnow.brk, &p_cuser->brk) ||
  347. put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  348. return -EFAULT;
  349. #endif
  350. return 0;
  351. default:
  352. ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
  353. }
  354. return ret;
  355. }