ircomm_tty_ioctl.c 11 KB

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