pty.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * Added support for a Unix98-style ptmx device.
  5. * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
  6. *
  7. * When reading this code see also fs/devpts. In particular note that the
  8. * driver_data field is used by the devpts side as a binding to the devpts
  9. * inode.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/major.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/bitops.h>
  25. #include <linux/devpts_fs.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #ifdef CONFIG_UNIX98_PTYS
  29. static struct tty_driver *ptm_driver;
  30. static struct tty_driver *pts_driver;
  31. static DEFINE_MUTEX(devpts_mutex);
  32. #endif
  33. static void pty_close(struct tty_struct *tty, struct file *filp)
  34. {
  35. BUG_ON(!tty);
  36. if (tty->driver->subtype == PTY_TYPE_MASTER)
  37. WARN_ON(tty->count > 1);
  38. else {
  39. if (tty->count > 2)
  40. return;
  41. }
  42. wake_up_interruptible(&tty->read_wait);
  43. wake_up_interruptible(&tty->write_wait);
  44. tty->packet = 0;
  45. if (!tty->link)
  46. return;
  47. tty->link->packet = 0;
  48. set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  49. wake_up_interruptible(&tty->link->read_wait);
  50. wake_up_interruptible(&tty->link->write_wait);
  51. if (tty->driver->subtype == PTY_TYPE_MASTER) {
  52. set_bit(TTY_OTHER_CLOSED, &tty->flags);
  53. #ifdef CONFIG_UNIX98_PTYS
  54. if (tty->driver == ptm_driver) {
  55. mutex_lock(&devpts_mutex);
  56. devpts_pty_kill(tty->link);
  57. mutex_unlock(&devpts_mutex);
  58. }
  59. #endif
  60. tty_unlock();
  61. tty_vhangup(tty->link);
  62. tty_lock();
  63. }
  64. }
  65. /*
  66. * The unthrottle routine is called by the line discipline to signal
  67. * that it can receive more characters. For PTY's, the TTY_THROTTLED
  68. * flag is always set, to force the line discipline to always call the
  69. * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
  70. * characters in the queue. This is necessary since each time this
  71. * happens, we need to wake up any sleeping processes that could be
  72. * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
  73. * for the pty buffer to be drained.
  74. */
  75. static void pty_unthrottle(struct tty_struct *tty)
  76. {
  77. tty_wakeup(tty->link);
  78. set_bit(TTY_THROTTLED, &tty->flags);
  79. }
  80. /**
  81. * pty_space - report space left for writing
  82. * @to: tty we are writing into
  83. *
  84. * The tty buffers allow 64K but we sneak a peak and clip at 8K this
  85. * allows a lot of overspill room for echo and other fun messes to
  86. * be handled properly
  87. */
  88. static int pty_space(struct tty_struct *to)
  89. {
  90. int n = 8192 - to->buf.memory_used;
  91. if (n < 0)
  92. return 0;
  93. return n;
  94. }
  95. /**
  96. * pty_write - write to a pty
  97. * @tty: the tty we write from
  98. * @buf: kernel buffer of data
  99. * @count: bytes to write
  100. *
  101. * Our "hardware" write method. Data is coming from the ldisc which
  102. * may be in a non sleeping state. We simply throw this at the other
  103. * end of the link as if we were an IRQ handler receiving stuff for
  104. * the other side of the pty/tty pair.
  105. */
  106. static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
  107. {
  108. struct tty_struct *to = tty->link;
  109. if (tty->stopped)
  110. return 0;
  111. if (c > 0) {
  112. /* Stuff the data into the input queue of the other end */
  113. c = tty_insert_flip_string(to, buf, c);
  114. /* And shovel */
  115. if (c) {
  116. tty_flip_buffer_push(to);
  117. tty_wakeup(tty);
  118. }
  119. }
  120. return c;
  121. }
  122. /**
  123. * pty_write_room - write space
  124. * @tty: tty we are writing from
  125. *
  126. * Report how many bytes the ldisc can send into the queue for
  127. * the other device.
  128. */
  129. static int pty_write_room(struct tty_struct *tty)
  130. {
  131. if (tty->stopped)
  132. return 0;
  133. return pty_space(tty->link);
  134. }
  135. /**
  136. * pty_chars_in_buffer - characters currently in our tx queue
  137. * @tty: our tty
  138. *
  139. * Report how much we have in the transmit queue. As everything is
  140. * instantly at the other end this is easy to implement.
  141. */
  142. static int pty_chars_in_buffer(struct tty_struct *tty)
  143. {
  144. return 0;
  145. }
  146. /* Set the lock flag on a pty */
  147. static int pty_set_lock(struct tty_struct *tty, int __user *arg)
  148. {
  149. int val;
  150. if (get_user(val, arg))
  151. return -EFAULT;
  152. if (val)
  153. set_bit(TTY_PTY_LOCK, &tty->flags);
  154. else
  155. clear_bit(TTY_PTY_LOCK, &tty->flags);
  156. return 0;
  157. }
  158. /* Send a signal to the slave */
  159. static int pty_signal(struct tty_struct *tty, int sig)
  160. {
  161. unsigned long flags;
  162. struct pid *pgrp;
  163. if (tty->link) {
  164. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  165. pgrp = get_pid(tty->link->pgrp);
  166. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  167. kill_pgrp(pgrp, sig, 1);
  168. put_pid(pgrp);
  169. }
  170. return 0;
  171. }
  172. static void pty_flush_buffer(struct tty_struct *tty)
  173. {
  174. struct tty_struct *to = tty->link;
  175. unsigned long flags;
  176. if (!to)
  177. return;
  178. /* tty_buffer_flush(to); FIXME */
  179. if (to->packet) {
  180. spin_lock_irqsave(&tty->ctrl_lock, flags);
  181. tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
  182. wake_up_interruptible(&to->read_wait);
  183. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  184. }
  185. }
  186. static int pty_open(struct tty_struct *tty, struct file *filp)
  187. {
  188. int retval = -ENODEV;
  189. if (!tty || !tty->link)
  190. goto out;
  191. retval = -EIO;
  192. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  193. goto out;
  194. if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
  195. goto out;
  196. if (tty->link->count != 1)
  197. goto out;
  198. clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
  199. set_bit(TTY_THROTTLED, &tty->flags);
  200. retval = 0;
  201. out:
  202. return retval;
  203. }
  204. static void pty_set_termios(struct tty_struct *tty,
  205. struct ktermios *old_termios)
  206. {
  207. tty->termios.c_cflag &= ~(CSIZE | PARENB);
  208. tty->termios.c_cflag |= (CS8 | CREAD);
  209. }
  210. /**
  211. * pty_do_resize - resize event
  212. * @tty: tty being resized
  213. * @ws: window size being set.
  214. *
  215. * Update the termios variables and send the necessary signals to
  216. * peform a terminal resize correctly
  217. */
  218. int pty_resize(struct tty_struct *tty, struct winsize *ws)
  219. {
  220. struct pid *pgrp, *rpgrp;
  221. unsigned long flags;
  222. struct tty_struct *pty = tty->link;
  223. /* For a PTY we need to lock the tty side */
  224. mutex_lock(&tty->termios_mutex);
  225. if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
  226. goto done;
  227. /* Get the PID values and reference them so we can
  228. avoid holding the tty ctrl lock while sending signals.
  229. We need to lock these individually however. */
  230. spin_lock_irqsave(&tty->ctrl_lock, flags);
  231. pgrp = get_pid(tty->pgrp);
  232. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  233. spin_lock_irqsave(&pty->ctrl_lock, flags);
  234. rpgrp = get_pid(pty->pgrp);
  235. spin_unlock_irqrestore(&pty->ctrl_lock, flags);
  236. if (pgrp)
  237. kill_pgrp(pgrp, SIGWINCH, 1);
  238. if (rpgrp != pgrp && rpgrp)
  239. kill_pgrp(rpgrp, SIGWINCH, 1);
  240. put_pid(pgrp);
  241. put_pid(rpgrp);
  242. tty->winsize = *ws;
  243. pty->winsize = *ws; /* Never used so will go away soon */
  244. done:
  245. mutex_unlock(&tty->termios_mutex);
  246. return 0;
  247. }
  248. static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty,
  249. bool legacy)
  250. {
  251. struct tty_struct *o_tty;
  252. struct tty_port *ports[2];
  253. int idx = tty->index;
  254. int retval = -ENOMEM;
  255. o_tty = alloc_tty_struct();
  256. ports[0] = kmalloc(sizeof **ports, GFP_KERNEL);
  257. ports[1] = kmalloc(sizeof **ports, GFP_KERNEL);
  258. if (!o_tty || !ports[0] || !ports[1])
  259. goto err_free_tty;
  260. if (!try_module_get(driver->other->owner)) {
  261. /* This cannot in fact currently happen */
  262. goto err_free_tty;
  263. }
  264. initialize_tty_struct(o_tty, driver->other, idx);
  265. if (legacy) {
  266. /* We always use new tty termios data so we can do this
  267. the easy way .. */
  268. retval = tty_init_termios(tty);
  269. if (retval)
  270. goto err_deinit_tty;
  271. retval = tty_init_termios(o_tty);
  272. if (retval)
  273. goto err_free_termios;
  274. driver->other->ttys[idx] = o_tty;
  275. driver->ttys[idx] = tty;
  276. } else {
  277. memset(&tty->termios_locked, 0, sizeof(tty->termios_locked));
  278. tty->termios = driver->init_termios;
  279. memset(&o_tty->termios_locked, 0, sizeof(tty->termios_locked));
  280. o_tty->termios = driver->other->init_termios;
  281. }
  282. /*
  283. * Everything allocated ... set up the o_tty structure.
  284. */
  285. tty_driver_kref_get(driver->other);
  286. if (driver->subtype == PTY_TYPE_MASTER)
  287. o_tty->count++;
  288. /* Establish the links in both directions */
  289. tty->link = o_tty;
  290. o_tty->link = tty;
  291. tty_port_init(ports[0]);
  292. tty_port_init(ports[1]);
  293. o_tty->port = ports[0];
  294. tty->port = ports[1];
  295. tty_driver_kref_get(driver);
  296. tty->count++;
  297. return 0;
  298. err_free_termios:
  299. if (legacy)
  300. tty_free_termios(tty);
  301. err_deinit_tty:
  302. deinitialize_tty_struct(o_tty);
  303. module_put(o_tty->driver->owner);
  304. err_free_tty:
  305. kfree(ports[0]);
  306. kfree(ports[1]);
  307. free_tty_struct(o_tty);
  308. return retval;
  309. }
  310. static void pty_cleanup(struct tty_struct *tty)
  311. {
  312. kfree(tty->port);
  313. }
  314. /* Traditional BSD devices */
  315. #ifdef CONFIG_LEGACY_PTYS
  316. static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
  317. {
  318. return pty_common_install(driver, tty, true);
  319. }
  320. static int pty_bsd_ioctl(struct tty_struct *tty,
  321. unsigned int cmd, unsigned long arg)
  322. {
  323. switch (cmd) {
  324. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  325. return pty_set_lock(tty, (int __user *) arg);
  326. case TIOCSIG: /* Send signal to other side of pty */
  327. return pty_signal(tty, (int) arg);
  328. }
  329. return -ENOIOCTLCMD;
  330. }
  331. static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
  332. module_param(legacy_count, int, 0);
  333. /*
  334. * The master side of a pty can do TIOCSPTLCK and thus
  335. * has pty_bsd_ioctl.
  336. */
  337. static const struct tty_operations master_pty_ops_bsd = {
  338. .install = pty_install,
  339. .open = pty_open,
  340. .close = pty_close,
  341. .write = pty_write,
  342. .write_room = pty_write_room,
  343. .flush_buffer = pty_flush_buffer,
  344. .chars_in_buffer = pty_chars_in_buffer,
  345. .unthrottle = pty_unthrottle,
  346. .set_termios = pty_set_termios,
  347. .ioctl = pty_bsd_ioctl,
  348. .cleanup = pty_cleanup,
  349. .resize = pty_resize
  350. };
  351. static const struct tty_operations slave_pty_ops_bsd = {
  352. .install = pty_install,
  353. .open = pty_open,
  354. .close = pty_close,
  355. .write = pty_write,
  356. .write_room = pty_write_room,
  357. .flush_buffer = pty_flush_buffer,
  358. .chars_in_buffer = pty_chars_in_buffer,
  359. .unthrottle = pty_unthrottle,
  360. .set_termios = pty_set_termios,
  361. .cleanup = pty_cleanup,
  362. .resize = pty_resize
  363. };
  364. static void __init legacy_pty_init(void)
  365. {
  366. struct tty_driver *pty_driver, *pty_slave_driver;
  367. if (legacy_count <= 0)
  368. return;
  369. pty_driver = alloc_tty_driver(legacy_count);
  370. if (!pty_driver)
  371. panic("Couldn't allocate pty driver");
  372. pty_slave_driver = alloc_tty_driver(legacy_count);
  373. if (!pty_slave_driver)
  374. panic("Couldn't allocate pty slave driver");
  375. pty_driver->driver_name = "pty_master";
  376. pty_driver->name = "pty";
  377. pty_driver->major = PTY_MASTER_MAJOR;
  378. pty_driver->minor_start = 0;
  379. pty_driver->type = TTY_DRIVER_TYPE_PTY;
  380. pty_driver->subtype = PTY_TYPE_MASTER;
  381. pty_driver->init_termios = tty_std_termios;
  382. pty_driver->init_termios.c_iflag = 0;
  383. pty_driver->init_termios.c_oflag = 0;
  384. pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  385. pty_driver->init_termios.c_lflag = 0;
  386. pty_driver->init_termios.c_ispeed = 38400;
  387. pty_driver->init_termios.c_ospeed = 38400;
  388. pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
  389. pty_driver->other = pty_slave_driver;
  390. tty_set_operations(pty_driver, &master_pty_ops_bsd);
  391. pty_slave_driver->driver_name = "pty_slave";
  392. pty_slave_driver->name = "ttyp";
  393. pty_slave_driver->major = PTY_SLAVE_MAJOR;
  394. pty_slave_driver->minor_start = 0;
  395. pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
  396. pty_slave_driver->subtype = PTY_TYPE_SLAVE;
  397. pty_slave_driver->init_termios = tty_std_termios;
  398. pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  399. pty_slave_driver->init_termios.c_ispeed = 38400;
  400. pty_slave_driver->init_termios.c_ospeed = 38400;
  401. pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
  402. TTY_DRIVER_REAL_RAW;
  403. pty_slave_driver->other = pty_driver;
  404. tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
  405. if (tty_register_driver(pty_driver))
  406. panic("Couldn't register pty driver");
  407. if (tty_register_driver(pty_slave_driver))
  408. panic("Couldn't register pty slave driver");
  409. }
  410. #else
  411. static inline void legacy_pty_init(void) { }
  412. #endif
  413. /* Unix98 devices */
  414. #ifdef CONFIG_UNIX98_PTYS
  415. static struct cdev ptmx_cdev;
  416. static int pty_unix98_ioctl(struct tty_struct *tty,
  417. unsigned int cmd, unsigned long arg)
  418. {
  419. switch (cmd) {
  420. case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
  421. return pty_set_lock(tty, (int __user *)arg);
  422. case TIOCGPTN: /* Get PT Number */
  423. return put_user(tty->index, (unsigned int __user *)arg);
  424. case TIOCSIG: /* Send signal to other side of pty */
  425. return pty_signal(tty, (int) arg);
  426. }
  427. return -ENOIOCTLCMD;
  428. }
  429. /**
  430. * ptm_unix98_lookup - find a pty master
  431. * @driver: ptm driver
  432. * @idx: tty index
  433. *
  434. * Look up a pty master device. Called under the tty_mutex for now.
  435. * This provides our locking.
  436. */
  437. static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  438. struct inode *ptm_inode, int idx)
  439. {
  440. /* Master must be open via /dev/ptmx */
  441. return ERR_PTR(-EIO);
  442. }
  443. /**
  444. * pts_unix98_lookup - find a pty slave
  445. * @driver: pts driver
  446. * @idx: tty index
  447. *
  448. * Look up a pty master device. Called under the tty_mutex for now.
  449. * This provides our locking for the tty pointer.
  450. */
  451. static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
  452. struct inode *pts_inode, int idx)
  453. {
  454. struct tty_struct *tty;
  455. mutex_lock(&devpts_mutex);
  456. tty = devpts_get_tty(pts_inode, idx);
  457. mutex_unlock(&devpts_mutex);
  458. /* Master must be open before slave */
  459. if (!tty)
  460. return ERR_PTR(-EIO);
  461. return tty;
  462. }
  463. /* We have no need to install and remove our tty objects as devpts does all
  464. the work for us */
  465. static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
  466. {
  467. return pty_common_install(driver, tty, false);
  468. }
  469. static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
  470. {
  471. }
  472. static const struct tty_operations ptm_unix98_ops = {
  473. .lookup = ptm_unix98_lookup,
  474. .install = pty_unix98_install,
  475. .remove = pty_unix98_remove,
  476. .open = pty_open,
  477. .close = pty_close,
  478. .write = pty_write,
  479. .write_room = pty_write_room,
  480. .flush_buffer = pty_flush_buffer,
  481. .chars_in_buffer = pty_chars_in_buffer,
  482. .unthrottle = pty_unthrottle,
  483. .set_termios = pty_set_termios,
  484. .ioctl = pty_unix98_ioctl,
  485. .resize = pty_resize,
  486. .cleanup = pty_cleanup
  487. };
  488. static const struct tty_operations pty_unix98_ops = {
  489. .lookup = pts_unix98_lookup,
  490. .install = pty_unix98_install,
  491. .remove = pty_unix98_remove,
  492. .open = pty_open,
  493. .close = pty_close,
  494. .write = pty_write,
  495. .write_room = pty_write_room,
  496. .flush_buffer = pty_flush_buffer,
  497. .chars_in_buffer = pty_chars_in_buffer,
  498. .unthrottle = pty_unthrottle,
  499. .set_termios = pty_set_termios,
  500. .cleanup = pty_cleanup,
  501. };
  502. /**
  503. * ptmx_open - open a unix 98 pty master
  504. * @inode: inode of device file
  505. * @filp: file pointer to tty
  506. *
  507. * Allocate a unix98 pty master device from the ptmx driver.
  508. *
  509. * Locking: tty_mutex protects the init_dev work. tty->count should
  510. * protect the rest.
  511. * allocated_ptys_lock handles the list of free pty numbers
  512. */
  513. static int ptmx_open(struct inode *inode, struct file *filp)
  514. {
  515. struct tty_struct *tty;
  516. int retval;
  517. int index;
  518. nonseekable_open(inode, filp);
  519. retval = tty_alloc_file(filp);
  520. if (retval)
  521. return retval;
  522. /* find a device that is not in use. */
  523. tty_lock();
  524. index = devpts_new_index(inode);
  525. tty_unlock();
  526. if (index < 0) {
  527. retval = index;
  528. goto err_file;
  529. }
  530. mutex_lock(&tty_mutex);
  531. mutex_lock(&devpts_mutex);
  532. tty = tty_init_dev(ptm_driver, index);
  533. mutex_unlock(&devpts_mutex);
  534. tty_lock();
  535. mutex_unlock(&tty_mutex);
  536. if (IS_ERR(tty)) {
  537. retval = PTR_ERR(tty);
  538. goto out;
  539. }
  540. set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
  541. tty_add_file(tty, filp);
  542. retval = devpts_pty_new(inode, tty->link);
  543. if (retval)
  544. goto err_release;
  545. retval = ptm_driver->ops->open(tty, filp);
  546. if (retval)
  547. goto err_release;
  548. tty_unlock();
  549. return 0;
  550. err_release:
  551. tty_unlock();
  552. tty_release(inode, filp);
  553. return retval;
  554. out:
  555. devpts_kill_index(inode, index);
  556. tty_unlock();
  557. err_file:
  558. tty_free_file(filp);
  559. return retval;
  560. }
  561. static struct file_operations ptmx_fops;
  562. static void __init unix98_pty_init(void)
  563. {
  564. ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  565. if (!ptm_driver)
  566. panic("Couldn't allocate Unix98 ptm driver");
  567. pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
  568. if (!pts_driver)
  569. panic("Couldn't allocate Unix98 pts driver");
  570. ptm_driver->driver_name = "pty_master";
  571. ptm_driver->name = "ptm";
  572. ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
  573. ptm_driver->minor_start = 0;
  574. ptm_driver->type = TTY_DRIVER_TYPE_PTY;
  575. ptm_driver->subtype = PTY_TYPE_MASTER;
  576. ptm_driver->init_termios = tty_std_termios;
  577. ptm_driver->init_termios.c_iflag = 0;
  578. ptm_driver->init_termios.c_oflag = 0;
  579. ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  580. ptm_driver->init_termios.c_lflag = 0;
  581. ptm_driver->init_termios.c_ispeed = 38400;
  582. ptm_driver->init_termios.c_ospeed = 38400;
  583. ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  584. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  585. ptm_driver->other = pts_driver;
  586. tty_set_operations(ptm_driver, &ptm_unix98_ops);
  587. pts_driver->driver_name = "pty_slave";
  588. pts_driver->name = "pts";
  589. pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
  590. pts_driver->minor_start = 0;
  591. pts_driver->type = TTY_DRIVER_TYPE_PTY;
  592. pts_driver->subtype = PTY_TYPE_SLAVE;
  593. pts_driver->init_termios = tty_std_termios;
  594. pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
  595. pts_driver->init_termios.c_ispeed = 38400;
  596. pts_driver->init_termios.c_ospeed = 38400;
  597. pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
  598. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
  599. pts_driver->other = ptm_driver;
  600. tty_set_operations(pts_driver, &pty_unix98_ops);
  601. if (tty_register_driver(ptm_driver))
  602. panic("Couldn't register Unix98 ptm driver");
  603. if (tty_register_driver(pts_driver))
  604. panic("Couldn't register Unix98 pts driver");
  605. /* Now create the /dev/ptmx special device */
  606. tty_default_fops(&ptmx_fops);
  607. ptmx_fops.open = ptmx_open;
  608. cdev_init(&ptmx_cdev, &ptmx_fops);
  609. if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
  610. register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
  611. panic("Couldn't register /dev/ptmx driver\n");
  612. device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
  613. }
  614. #else
  615. static inline void unix98_pty_init(void) { }
  616. #endif
  617. static int __init pty_init(void)
  618. {
  619. legacy_pty_init();
  620. unix98_pty_init();
  621. return 0;
  622. }
  623. module_init(pty_init);