ircomm_tty_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. tty_port_set_cts_flow(&self->port, cflag & CRTSCTS);
  77. if (cflag & CRTSCTS) {
  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->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
  85. }
  86. tty_port_set_check_carrier(&self->port, ~cflag & CLOCAL);
  87. #if 0
  88. /*
  89. * Set up parity check flag
  90. */
  91. if (I_INPCK(self->tty))
  92. driver->read_status_mask |= LSR_FE | LSR_PE;
  93. if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
  94. driver->read_status_mask |= LSR_BI;
  95. /*
  96. * Characters to ignore
  97. */
  98. driver->ignore_status_mask = 0;
  99. if (I_IGNPAR(driver->tty))
  100. driver->ignore_status_mask |= LSR_PE | LSR_FE;
  101. if (I_IGNBRK(self->tty)) {
  102. self->ignore_status_mask |= LSR_BI;
  103. /*
  104. * If we're ignore parity and break indicators, ignore
  105. * overruns too. (For real raw support).
  106. */
  107. if (I_IGNPAR(self->tty))
  108. self->ignore_status_mask |= LSR_OE;
  109. }
  110. #endif
  111. self->settings.data_format = cval;
  112. ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
  113. ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
  114. }
  115. /*
  116. * Function ircomm_tty_set_termios (tty, old_termios)
  117. *
  118. * This routine allows the tty driver to be notified when device's
  119. * termios settings have changed. Note that a well-designed tty driver
  120. * should be prepared to accept the case where old == NULL, and try to
  121. * do something rational.
  122. */
  123. void ircomm_tty_set_termios(struct tty_struct *tty,
  124. struct ktermios *old_termios)
  125. {
  126. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  127. unsigned int cflag = tty->termios.c_cflag;
  128. if ((cflag == old_termios->c_cflag) &&
  129. (RELEVANT_IFLAG(tty->termios.c_iflag) ==
  130. RELEVANT_IFLAG(old_termios->c_iflag)))
  131. {
  132. return;
  133. }
  134. ircomm_tty_change_speed(self, tty);
  135. /* Handle transition to B0 status */
  136. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
  137. self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
  138. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  139. }
  140. /* Handle transition away from B0 status */
  141. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  142. self->settings.dte |= IRCOMM_DTR;
  143. if (!C_CRTSCTS(tty) || !tty_throttled(tty))
  144. self->settings.dte |= IRCOMM_RTS;
  145. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  146. }
  147. /* Handle turning off CRTSCTS */
  148. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty))
  149. {
  150. tty->hw_stopped = 0;
  151. ircomm_tty_start(tty);
  152. }
  153. }
  154. /*
  155. * Function ircomm_tty_tiocmget (tty)
  156. *
  157. *
  158. *
  159. */
  160. int ircomm_tty_tiocmget(struct tty_struct *tty)
  161. {
  162. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  163. unsigned int result;
  164. if (tty_io_error(tty))
  165. return -EIO;
  166. result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
  167. | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
  168. | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
  169. | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
  170. | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
  171. | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
  172. return result;
  173. }
  174. /*
  175. * Function ircomm_tty_tiocmset (tty, set, clear)
  176. *
  177. *
  178. *
  179. */
  180. int ircomm_tty_tiocmset(struct tty_struct *tty,
  181. unsigned int set, unsigned int clear)
  182. {
  183. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  184. if (tty_io_error(tty))
  185. return -EIO;
  186. IRDA_ASSERT(self != NULL, return -1;);
  187. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  188. if (set & TIOCM_RTS)
  189. self->settings.dte |= IRCOMM_RTS;
  190. if (set & TIOCM_DTR)
  191. self->settings.dte |= IRCOMM_DTR;
  192. if (clear & TIOCM_RTS)
  193. self->settings.dte &= ~IRCOMM_RTS;
  194. if (clear & TIOCM_DTR)
  195. self->settings.dte &= ~IRCOMM_DTR;
  196. if ((set|clear) & TIOCM_RTS)
  197. self->settings.dte |= IRCOMM_DELTA_RTS;
  198. if ((set|clear) & TIOCM_DTR)
  199. self->settings.dte |= IRCOMM_DELTA_DTR;
  200. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  201. return 0;
  202. }
  203. /*
  204. * Function get_serial_info (driver, retinfo)
  205. *
  206. *
  207. *
  208. */
  209. static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
  210. struct serial_struct __user *retinfo)
  211. {
  212. struct serial_struct info;
  213. memset(&info, 0, sizeof(info));
  214. info.line = self->line;
  215. info.flags = self->port.flags;
  216. info.baud_base = self->settings.data_rate;
  217. info.close_delay = self->port.close_delay;
  218. info.closing_wait = self->port.closing_wait;
  219. /* For compatibility */
  220. info.type = PORT_16550A;
  221. if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
  222. return -EFAULT;
  223. return 0;
  224. }
  225. /*
  226. * Function set_serial_info (driver, new_info)
  227. *
  228. *
  229. *
  230. */
  231. static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
  232. struct serial_struct __user *new_info)
  233. {
  234. #if 0
  235. struct serial_struct new_serial;
  236. struct ircomm_tty_cb old_state, *state;
  237. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  238. return -EFAULT;
  239. state = self
  240. old_state = *self;
  241. if (!capable(CAP_SYS_ADMIN)) {
  242. if ((new_serial.baud_base != state->settings.data_rate) ||
  243. (new_serial.close_delay != state->close_delay) ||
  244. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  245. (self->flags & ~ASYNC_USR_MASK)))
  246. return -EPERM;
  247. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  248. (new_serial.flags & ASYNC_USR_MASK));
  249. self->flags = ((self->flags & ~ASYNC_USR_MASK) |
  250. (new_serial.flags & ASYNC_USR_MASK));
  251. /* self->custom_divisor = new_serial.custom_divisor; */
  252. goto check_and_exit;
  253. }
  254. /*
  255. * OK, past this point, all the error checking has been done.
  256. * At this point, we start making changes.....
  257. */
  258. if (self->settings.data_rate != new_serial.baud_base) {
  259. self->settings.data_rate = new_serial.baud_base;
  260. ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
  261. }
  262. self->close_delay = new_serial.close_delay * HZ/100;
  263. self->closing_wait = new_serial.closing_wait * HZ/100;
  264. /* self->custom_divisor = new_serial.custom_divisor; */
  265. self->flags = ((self->flags & ~ASYNC_FLAGS) |
  266. (new_serial.flags & ASYNC_FLAGS));
  267. self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  268. check_and_exit:
  269. if (tty_port_initialized(self)) {
  270. if (((old_state.flags & ASYNC_SPD_MASK) !=
  271. (self->flags & ASYNC_SPD_MASK)) ||
  272. (old_driver.custom_divisor != driver->custom_divisor)) {
  273. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  274. driver->tty->alt_speed = 57600;
  275. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  276. driver->tty->alt_speed = 115200;
  277. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  278. driver->tty->alt_speed = 230400;
  279. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  280. driver->tty->alt_speed = 460800;
  281. ircomm_tty_change_speed(driver);
  282. }
  283. }
  284. #endif
  285. return 0;
  286. }
  287. /*
  288. * Function ircomm_tty_ioctl (tty, cmd, arg)
  289. *
  290. *
  291. *
  292. */
  293. int ircomm_tty_ioctl(struct tty_struct *tty,
  294. unsigned int cmd, unsigned long arg)
  295. {
  296. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  297. int ret = 0;
  298. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  299. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  300. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  301. if (tty_io_error(tty))
  302. return -EIO;
  303. }
  304. switch (cmd) {
  305. case TIOCGSERIAL:
  306. ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
  307. break;
  308. case TIOCSSERIAL:
  309. ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
  310. break;
  311. case TIOCMIWAIT:
  312. pr_debug("(), TIOCMIWAIT, not impl!\n");
  313. break;
  314. case TIOCGICOUNT:
  315. pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__);
  316. #if 0
  317. save_flags(flags); cli();
  318. cnow = driver->icount;
  319. restore_flags(flags);
  320. p_cuser = (struct serial_icounter_struct __user *) arg;
  321. if (put_user(cnow.cts, &p_cuser->cts) ||
  322. put_user(cnow.dsr, &p_cuser->dsr) ||
  323. put_user(cnow.rng, &p_cuser->rng) ||
  324. put_user(cnow.dcd, &p_cuser->dcd) ||
  325. put_user(cnow.rx, &p_cuser->rx) ||
  326. put_user(cnow.tx, &p_cuser->tx) ||
  327. put_user(cnow.frame, &p_cuser->frame) ||
  328. put_user(cnow.overrun, &p_cuser->overrun) ||
  329. put_user(cnow.parity, &p_cuser->parity) ||
  330. put_user(cnow.brk, &p_cuser->brk) ||
  331. put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  332. return -EFAULT;
  333. #endif
  334. return 0;
  335. default:
  336. ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
  337. }
  338. return ret;
  339. }