ircomm_tty_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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) && !(cflag & CBAUD)) {
  141. self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
  142. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  143. }
  144. /* Handle transition away from B0 status */
  145. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  146. self->settings.dte |= IRCOMM_DTR;
  147. if (!C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
  148. self->settings.dte |= IRCOMM_RTS;
  149. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  150. }
  151. /* Handle turning off CRTSCTS */
  152. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty))
  153. {
  154. tty->hw_stopped = 0;
  155. ircomm_tty_start(tty);
  156. }
  157. }
  158. /*
  159. * Function ircomm_tty_tiocmget (tty)
  160. *
  161. *
  162. *
  163. */
  164. int ircomm_tty_tiocmget(struct tty_struct *tty)
  165. {
  166. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  167. unsigned int result;
  168. if (tty->flags & (1 << TTY_IO_ERROR))
  169. return -EIO;
  170. result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
  171. | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
  172. | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
  173. | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
  174. | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
  175. | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
  176. return result;
  177. }
  178. /*
  179. * Function ircomm_tty_tiocmset (tty, set, clear)
  180. *
  181. *
  182. *
  183. */
  184. int ircomm_tty_tiocmset(struct tty_struct *tty,
  185. unsigned int set, unsigned int clear)
  186. {
  187. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  188. if (tty->flags & (1 << TTY_IO_ERROR))
  189. return -EIO;
  190. IRDA_ASSERT(self != NULL, return -1;);
  191. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  192. if (set & TIOCM_RTS)
  193. self->settings.dte |= IRCOMM_RTS;
  194. if (set & TIOCM_DTR)
  195. self->settings.dte |= IRCOMM_DTR;
  196. if (clear & TIOCM_RTS)
  197. self->settings.dte &= ~IRCOMM_RTS;
  198. if (clear & TIOCM_DTR)
  199. self->settings.dte &= ~IRCOMM_DTR;
  200. if ((set|clear) & TIOCM_RTS)
  201. self->settings.dte |= IRCOMM_DELTA_RTS;
  202. if ((set|clear) & TIOCM_DTR)
  203. self->settings.dte |= IRCOMM_DELTA_DTR;
  204. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  205. return 0;
  206. }
  207. /*
  208. * Function get_serial_info (driver, retinfo)
  209. *
  210. *
  211. *
  212. */
  213. static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
  214. struct serial_struct __user *retinfo)
  215. {
  216. struct serial_struct info;
  217. if (!retinfo)
  218. return -EFAULT;
  219. memset(&info, 0, sizeof(info));
  220. info.line = self->line;
  221. info.flags = self->port.flags;
  222. info.baud_base = self->settings.data_rate;
  223. info.close_delay = self->port.close_delay;
  224. info.closing_wait = self->port.closing_wait;
  225. /* For compatibility */
  226. info.type = PORT_16550A;
  227. info.port = 0;
  228. info.irq = 0;
  229. info.xmit_fifo_size = 0;
  230. info.hub6 = 0;
  231. info.custom_divisor = 0;
  232. if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
  233. return -EFAULT;
  234. return 0;
  235. }
  236. /*
  237. * Function set_serial_info (driver, new_info)
  238. *
  239. *
  240. *
  241. */
  242. static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
  243. struct serial_struct __user *new_info)
  244. {
  245. #if 0
  246. struct serial_struct new_serial;
  247. struct ircomm_tty_cb old_state, *state;
  248. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  249. return -EFAULT;
  250. state = self
  251. old_state = *self;
  252. if (!capable(CAP_SYS_ADMIN)) {
  253. if ((new_serial.baud_base != state->settings.data_rate) ||
  254. (new_serial.close_delay != state->close_delay) ||
  255. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  256. (self->flags & ~ASYNC_USR_MASK)))
  257. return -EPERM;
  258. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  259. (new_serial.flags & ASYNC_USR_MASK));
  260. self->flags = ((self->flags & ~ASYNC_USR_MASK) |
  261. (new_serial.flags & ASYNC_USR_MASK));
  262. /* self->custom_divisor = new_serial.custom_divisor; */
  263. goto check_and_exit;
  264. }
  265. /*
  266. * OK, past this point, all the error checking has been done.
  267. * At this point, we start making changes.....
  268. */
  269. if (self->settings.data_rate != new_serial.baud_base) {
  270. self->settings.data_rate = new_serial.baud_base;
  271. ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
  272. }
  273. self->close_delay = new_serial.close_delay * HZ/100;
  274. self->closing_wait = new_serial.closing_wait * HZ/100;
  275. /* self->custom_divisor = new_serial.custom_divisor; */
  276. self->flags = ((self->flags & ~ASYNC_FLAGS) |
  277. (new_serial.flags & ASYNC_FLAGS));
  278. self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  279. check_and_exit:
  280. if (self->flags & ASYNC_INITIALIZED) {
  281. if (((old_state.flags & ASYNC_SPD_MASK) !=
  282. (self->flags & ASYNC_SPD_MASK)) ||
  283. (old_driver.custom_divisor != driver->custom_divisor)) {
  284. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  285. driver->tty->alt_speed = 57600;
  286. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  287. driver->tty->alt_speed = 115200;
  288. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  289. driver->tty->alt_speed = 230400;
  290. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  291. driver->tty->alt_speed = 460800;
  292. ircomm_tty_change_speed(driver);
  293. }
  294. }
  295. #endif
  296. return 0;
  297. }
  298. /*
  299. * Function ircomm_tty_ioctl (tty, cmd, arg)
  300. *
  301. *
  302. *
  303. */
  304. int ircomm_tty_ioctl(struct tty_struct *tty,
  305. unsigned int cmd, unsigned long arg)
  306. {
  307. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  308. int ret = 0;
  309. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  310. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  311. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  312. if (tty->flags & (1 << TTY_IO_ERROR))
  313. return -EIO;
  314. }
  315. switch (cmd) {
  316. case TIOCGSERIAL:
  317. ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
  318. break;
  319. case TIOCSSERIAL:
  320. ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
  321. break;
  322. case TIOCMIWAIT:
  323. pr_debug("(), TIOCMIWAIT, not impl!\n");
  324. break;
  325. case TIOCGICOUNT:
  326. pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__);
  327. #if 0
  328. save_flags(flags); cli();
  329. cnow = driver->icount;
  330. restore_flags(flags);
  331. p_cuser = (struct serial_icounter_struct __user *) arg;
  332. if (put_user(cnow.cts, &p_cuser->cts) ||
  333. put_user(cnow.dsr, &p_cuser->dsr) ||
  334. put_user(cnow.rng, &p_cuser->rng) ||
  335. put_user(cnow.dcd, &p_cuser->dcd) ||
  336. put_user(cnow.rx, &p_cuser->rx) ||
  337. put_user(cnow.tx, &p_cuser->tx) ||
  338. put_user(cnow.frame, &p_cuser->frame) ||
  339. put_user(cnow.overrun, &p_cuser->overrun) ||
  340. put_user(cnow.parity, &p_cuser->parity) ||
  341. put_user(cnow.brk, &p_cuser->brk) ||
  342. put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  343. return -EFAULT;
  344. #endif
  345. return 0;
  346. default:
  347. ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
  348. }
  349. return ret;
  350. }