ircomm_tty_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. IRDA_DEBUG(2, "%s()\n", __func__ );
  53. if (!self->ircomm)
  54. return;
  55. cflag = tty->termios.c_cflag;
  56. /* byte size and parity */
  57. switch (cflag & CSIZE) {
  58. case CS5: cval = IRCOMM_WSIZE_5; break;
  59. case CS6: cval = IRCOMM_WSIZE_6; break;
  60. case CS7: cval = IRCOMM_WSIZE_7; break;
  61. case CS8: cval = IRCOMM_WSIZE_8; break;
  62. default: cval = IRCOMM_WSIZE_5; break;
  63. }
  64. if (cflag & CSTOPB)
  65. cval |= IRCOMM_2_STOP_BIT;
  66. if (cflag & PARENB)
  67. cval |= IRCOMM_PARITY_ENABLE;
  68. if (!(cflag & PARODD))
  69. cval |= IRCOMM_PARITY_EVEN;
  70. /* Determine divisor based on baud rate */
  71. baud = tty_get_baud_rate(tty);
  72. if (!baud)
  73. baud = 9600; /* B0 transition handled in rs_set_termios */
  74. self->settings.data_rate = baud;
  75. ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
  76. /* CTS flow control flag and modem status interrupts */
  77. if (cflag & CRTSCTS) {
  78. self->port.flags |= ASYNC_CTS_FLOW;
  79. self->settings.flow_control |= IRCOMM_RTS_CTS_IN;
  80. /* This got me. Bummer. Jean II */
  81. if (self->service_type == IRCOMM_3_WIRE_RAW)
  82. IRDA_WARNING("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n", __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. IRDA_DEBUG(2, "%s()\n", __func__ );
  133. if ((cflag == old_termios->c_cflag) &&
  134. (RELEVANT_IFLAG(tty->termios.c_iflag) ==
  135. RELEVANT_IFLAG(old_termios->c_iflag)))
  136. {
  137. return;
  138. }
  139. ircomm_tty_change_speed(self, tty);
  140. /* Handle transition to B0 status */
  141. if ((old_termios->c_cflag & CBAUD) &&
  142. !(cflag & CBAUD)) {
  143. self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
  144. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  145. }
  146. /* Handle transition away from B0 status */
  147. if (!(old_termios->c_cflag & CBAUD) &&
  148. (cflag & CBAUD)) {
  149. self->settings.dte |= IRCOMM_DTR;
  150. if (!(tty->termios.c_cflag & CRTSCTS) ||
  151. !test_bit(TTY_THROTTLED, &tty->flags)) {
  152. self->settings.dte |= IRCOMM_RTS;
  153. }
  154. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  155. }
  156. /* Handle turning off CRTSCTS */
  157. if ((old_termios->c_cflag & CRTSCTS) &&
  158. !(tty->termios.c_cflag & CRTSCTS))
  159. {
  160. tty->hw_stopped = 0;
  161. ircomm_tty_start(tty);
  162. }
  163. }
  164. /*
  165. * Function ircomm_tty_tiocmget (tty)
  166. *
  167. *
  168. *
  169. */
  170. int ircomm_tty_tiocmget(struct tty_struct *tty)
  171. {
  172. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  173. unsigned int result;
  174. IRDA_DEBUG(2, "%s()\n", __func__ );
  175. if (tty->flags & (1 << TTY_IO_ERROR))
  176. return -EIO;
  177. result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
  178. | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
  179. | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
  180. | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
  181. | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
  182. | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
  183. return result;
  184. }
  185. /*
  186. * Function ircomm_tty_tiocmset (tty, set, clear)
  187. *
  188. *
  189. *
  190. */
  191. int ircomm_tty_tiocmset(struct tty_struct *tty,
  192. unsigned int set, unsigned int clear)
  193. {
  194. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  195. IRDA_DEBUG(2, "%s()\n", __func__ );
  196. if (tty->flags & (1 << TTY_IO_ERROR))
  197. return -EIO;
  198. IRDA_ASSERT(self != NULL, return -1;);
  199. IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
  200. if (set & TIOCM_RTS)
  201. self->settings.dte |= IRCOMM_RTS;
  202. if (set & TIOCM_DTR)
  203. self->settings.dte |= IRCOMM_DTR;
  204. if (clear & TIOCM_RTS)
  205. self->settings.dte &= ~IRCOMM_RTS;
  206. if (clear & TIOCM_DTR)
  207. self->settings.dte &= ~IRCOMM_DTR;
  208. if ((set|clear) & TIOCM_RTS)
  209. self->settings.dte |= IRCOMM_DELTA_RTS;
  210. if ((set|clear) & TIOCM_DTR)
  211. self->settings.dte |= IRCOMM_DELTA_DTR;
  212. ircomm_param_request(self, IRCOMM_DTE, TRUE);
  213. return 0;
  214. }
  215. /*
  216. * Function get_serial_info (driver, retinfo)
  217. *
  218. *
  219. *
  220. */
  221. static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
  222. struct serial_struct __user *retinfo)
  223. {
  224. struct serial_struct info;
  225. if (!retinfo)
  226. return -EFAULT;
  227. IRDA_DEBUG(2, "%s()\n", __func__ );
  228. memset(&info, 0, sizeof(info));
  229. info.line = self->line;
  230. info.flags = self->port.flags;
  231. info.baud_base = self->settings.data_rate;
  232. info.close_delay = self->port.close_delay;
  233. info.closing_wait = self->port.closing_wait;
  234. /* For compatibility */
  235. info.type = PORT_16550A;
  236. info.port = 0;
  237. info.irq = 0;
  238. info.xmit_fifo_size = 0;
  239. info.hub6 = 0;
  240. info.custom_divisor = 0;
  241. if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
  242. return -EFAULT;
  243. return 0;
  244. }
  245. /*
  246. * Function set_serial_info (driver, new_info)
  247. *
  248. *
  249. *
  250. */
  251. static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
  252. struct serial_struct __user *new_info)
  253. {
  254. #if 0
  255. struct serial_struct new_serial;
  256. struct ircomm_tty_cb old_state, *state;
  257. IRDA_DEBUG(0, "%s()\n", __func__ );
  258. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  259. return -EFAULT;
  260. state = self
  261. old_state = *self;
  262. if (!capable(CAP_SYS_ADMIN)) {
  263. if ((new_serial.baud_base != state->settings.data_rate) ||
  264. (new_serial.close_delay != state->close_delay) ||
  265. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  266. (self->flags & ~ASYNC_USR_MASK)))
  267. return -EPERM;
  268. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  269. (new_serial.flags & ASYNC_USR_MASK));
  270. self->flags = ((self->flags & ~ASYNC_USR_MASK) |
  271. (new_serial.flags & ASYNC_USR_MASK));
  272. /* self->custom_divisor = new_serial.custom_divisor; */
  273. goto check_and_exit;
  274. }
  275. /*
  276. * OK, past this point, all the error checking has been done.
  277. * At this point, we start making changes.....
  278. */
  279. if (self->settings.data_rate != new_serial.baud_base) {
  280. self->settings.data_rate = new_serial.baud_base;
  281. ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
  282. }
  283. self->close_delay = new_serial.close_delay * HZ/100;
  284. self->closing_wait = new_serial.closing_wait * HZ/100;
  285. /* self->custom_divisor = new_serial.custom_divisor; */
  286. self->flags = ((self->flags & ~ASYNC_FLAGS) |
  287. (new_serial.flags & ASYNC_FLAGS));
  288. self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  289. check_and_exit:
  290. if (self->flags & ASYNC_INITIALIZED) {
  291. if (((old_state.flags & ASYNC_SPD_MASK) !=
  292. (self->flags & ASYNC_SPD_MASK)) ||
  293. (old_driver.custom_divisor != driver->custom_divisor)) {
  294. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  295. driver->tty->alt_speed = 57600;
  296. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  297. driver->tty->alt_speed = 115200;
  298. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  299. driver->tty->alt_speed = 230400;
  300. if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  301. driver->tty->alt_speed = 460800;
  302. ircomm_tty_change_speed(driver);
  303. }
  304. }
  305. #endif
  306. return 0;
  307. }
  308. /*
  309. * Function ircomm_tty_ioctl (tty, cmd, arg)
  310. *
  311. *
  312. *
  313. */
  314. int ircomm_tty_ioctl(struct tty_struct *tty,
  315. unsigned int cmd, unsigned long arg)
  316. {
  317. struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
  318. int ret = 0;
  319. IRDA_DEBUG(2, "%s()\n", __func__ );
  320. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  321. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  322. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  323. if (tty->flags & (1 << TTY_IO_ERROR))
  324. return -EIO;
  325. }
  326. switch (cmd) {
  327. case TIOCGSERIAL:
  328. ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
  329. break;
  330. case TIOCSSERIAL:
  331. ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
  332. break;
  333. case TIOCMIWAIT:
  334. IRDA_DEBUG(0, "(), TIOCMIWAIT, not impl!\n");
  335. break;
  336. case TIOCGICOUNT:
  337. IRDA_DEBUG(0, "%s(), TIOCGICOUNT not impl!\n", __func__ );
  338. #if 0
  339. save_flags(flags); cli();
  340. cnow = driver->icount;
  341. restore_flags(flags);
  342. p_cuser = (struct serial_icounter_struct __user *) arg;
  343. if (put_user(cnow.cts, &p_cuser->cts) ||
  344. put_user(cnow.dsr, &p_cuser->dsr) ||
  345. put_user(cnow.rng, &p_cuser->rng) ||
  346. put_user(cnow.dcd, &p_cuser->dcd) ||
  347. put_user(cnow.rx, &p_cuser->rx) ||
  348. put_user(cnow.tx, &p_cuser->tx) ||
  349. put_user(cnow.frame, &p_cuser->frame) ||
  350. put_user(cnow.overrun, &p_cuser->overrun) ||
  351. put_user(cnow.parity, &p_cuser->parity) ||
  352. put_user(cnow.brk, &p_cuser->brk) ||
  353. put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  354. return -EFAULT;
  355. #endif
  356. return 0;
  357. default:
  358. ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
  359. }
  360. return ret;
  361. }