pty.c 18 KB

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