pty.c 18 KB

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