tty.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IPWireless 3G PCMCIA Network Driver
  4. *
  5. * Original code
  6. * by Stephen Blackheath <stephen@blacksapphire.com>,
  7. * Ben Martel <benm@symmetric.co.nz>
  8. *
  9. * Copyrighted as follows:
  10. * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
  11. *
  12. * Various driver changes and rewrites, port to new kernels
  13. * Copyright (C) 2006-2007 Jiri Kosina
  14. *
  15. * Misc code cleanups and updates
  16. * Copyright (C) 2007 David Sterba
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/ppp_defs.h>
  22. #include <linux/if.h>
  23. #include <linux/ppp-ioctl.h>
  24. #include <linux/sched.h>
  25. #include <linux/serial.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/uaccess.h>
  31. #include "tty.h"
  32. #include "network.h"
  33. #include "hardware.h"
  34. #include "main.h"
  35. #define IPWIRELESS_PCMCIA_START (0)
  36. #define IPWIRELESS_PCMCIA_MINORS (24)
  37. #define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
  38. #define TTYTYPE_MODEM (0)
  39. #define TTYTYPE_MONITOR (1)
  40. #define TTYTYPE_RAS_RAW (2)
  41. struct ipw_tty {
  42. struct tty_port port;
  43. int index;
  44. struct ipw_hardware *hardware;
  45. unsigned int channel_idx;
  46. unsigned int secondary_channel_idx;
  47. int tty_type;
  48. struct ipw_network *network;
  49. unsigned int control_lines;
  50. struct mutex ipw_tty_mutex;
  51. int tx_bytes_queued;
  52. int closing;
  53. };
  54. static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
  55. static struct tty_driver *ipw_tty_driver;
  56. static char *tty_type_name(int tty_type)
  57. {
  58. static char *channel_names[] = {
  59. "modem",
  60. "monitor",
  61. "RAS-raw"
  62. };
  63. return channel_names[tty_type];
  64. }
  65. static struct ipw_tty *get_tty(int index)
  66. {
  67. /*
  68. * The 'ras_raw' channel is only available when 'loopback' mode
  69. * is enabled.
  70. * Number of minor starts with 16 (_RANGE * _RAS_RAW).
  71. */
  72. if (!ipwireless_loopback && index >=
  73. IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
  74. return NULL;
  75. return ttys[index];
  76. }
  77. static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
  78. {
  79. struct ipw_tty *tty = get_tty(linux_tty->index);
  80. if (!tty)
  81. return -ENODEV;
  82. mutex_lock(&tty->ipw_tty_mutex);
  83. if (tty->port.count == 0)
  84. tty->tx_bytes_queued = 0;
  85. tty->port.count++;
  86. tty->port.tty = linux_tty;
  87. linux_tty->driver_data = tty;
  88. tty->port.low_latency = 1;
  89. if (tty->tty_type == TTYTYPE_MODEM)
  90. ipwireless_ppp_open(tty->network);
  91. mutex_unlock(&tty->ipw_tty_mutex);
  92. return 0;
  93. }
  94. static void do_ipw_close(struct ipw_tty *tty)
  95. {
  96. tty->port.count--;
  97. if (tty->port.count == 0) {
  98. struct tty_struct *linux_tty = tty->port.tty;
  99. if (linux_tty != NULL) {
  100. tty->port.tty = NULL;
  101. linux_tty->driver_data = NULL;
  102. if (tty->tty_type == TTYTYPE_MODEM)
  103. ipwireless_ppp_close(tty->network);
  104. }
  105. }
  106. }
  107. static void ipw_hangup(struct tty_struct *linux_tty)
  108. {
  109. struct ipw_tty *tty = linux_tty->driver_data;
  110. if (!tty)
  111. return;
  112. mutex_lock(&tty->ipw_tty_mutex);
  113. if (tty->port.count == 0) {
  114. mutex_unlock(&tty->ipw_tty_mutex);
  115. return;
  116. }
  117. do_ipw_close(tty);
  118. mutex_unlock(&tty->ipw_tty_mutex);
  119. }
  120. static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
  121. {
  122. ipw_hangup(linux_tty);
  123. }
  124. /* Take data received from hardware, and send it out the tty */
  125. void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
  126. unsigned int length)
  127. {
  128. int work = 0;
  129. mutex_lock(&tty->ipw_tty_mutex);
  130. if (!tty->port.count) {
  131. mutex_unlock(&tty->ipw_tty_mutex);
  132. return;
  133. }
  134. mutex_unlock(&tty->ipw_tty_mutex);
  135. work = tty_insert_flip_string(&tty->port, data, length);
  136. if (work != length)
  137. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  138. ": %d chars not inserted to flip buffer!\n",
  139. length - work);
  140. if (work)
  141. tty_flip_buffer_push(&tty->port);
  142. }
  143. static void ipw_write_packet_sent_callback(void *callback_data,
  144. unsigned int packet_length)
  145. {
  146. struct ipw_tty *tty = callback_data;
  147. /*
  148. * Packet has been sent, so we subtract the number of bytes from our
  149. * tally of outstanding TX bytes.
  150. */
  151. tty->tx_bytes_queued -= packet_length;
  152. }
  153. static int ipw_write(struct tty_struct *linux_tty,
  154. const unsigned char *buf, int count)
  155. {
  156. struct ipw_tty *tty = linux_tty->driver_data;
  157. int room, ret;
  158. if (!tty)
  159. return -ENODEV;
  160. mutex_lock(&tty->ipw_tty_mutex);
  161. if (!tty->port.count) {
  162. mutex_unlock(&tty->ipw_tty_mutex);
  163. return -EINVAL;
  164. }
  165. room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
  166. if (room < 0)
  167. room = 0;
  168. /* Don't allow caller to write any more than we have room for */
  169. if (count > room)
  170. count = room;
  171. if (count == 0) {
  172. mutex_unlock(&tty->ipw_tty_mutex);
  173. return 0;
  174. }
  175. ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
  176. buf, count,
  177. ipw_write_packet_sent_callback, tty);
  178. if (ret < 0) {
  179. mutex_unlock(&tty->ipw_tty_mutex);
  180. return 0;
  181. }
  182. tty->tx_bytes_queued += count;
  183. mutex_unlock(&tty->ipw_tty_mutex);
  184. return count;
  185. }
  186. static int ipw_write_room(struct tty_struct *linux_tty)
  187. {
  188. struct ipw_tty *tty = linux_tty->driver_data;
  189. int room;
  190. /* FIXME: Exactly how is the tty object locked here .. */
  191. if (!tty)
  192. return -ENODEV;
  193. if (!tty->port.count)
  194. return -EINVAL;
  195. room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
  196. if (room < 0)
  197. room = 0;
  198. return room;
  199. }
  200. static int ipwireless_get_serial_info(struct tty_struct *linux_tty,
  201. struct serial_struct *ss)
  202. {
  203. struct ipw_tty *tty = linux_tty->driver_data;
  204. if (!tty)
  205. return -ENODEV;
  206. if (!tty->port.count)
  207. return -EINVAL;
  208. ss->type = PORT_UNKNOWN;
  209. ss->line = tty->index;
  210. ss->baud_base = 115200;
  211. return 0;
  212. }
  213. static int ipwireless_set_serial_info(struct tty_struct *linux_tty,
  214. struct serial_struct *ss)
  215. {
  216. return 0; /* Keeps the PCMCIA scripts happy. */
  217. }
  218. static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
  219. {
  220. struct ipw_tty *tty = linux_tty->driver_data;
  221. if (!tty)
  222. return 0;
  223. if (!tty->port.count)
  224. return 0;
  225. return tty->tx_bytes_queued;
  226. }
  227. static int get_control_lines(struct ipw_tty *tty)
  228. {
  229. unsigned int my = tty->control_lines;
  230. unsigned int out = 0;
  231. if (my & IPW_CONTROL_LINE_RTS)
  232. out |= TIOCM_RTS;
  233. if (my & IPW_CONTROL_LINE_DTR)
  234. out |= TIOCM_DTR;
  235. if (my & IPW_CONTROL_LINE_CTS)
  236. out |= TIOCM_CTS;
  237. if (my & IPW_CONTROL_LINE_DSR)
  238. out |= TIOCM_DSR;
  239. if (my & IPW_CONTROL_LINE_DCD)
  240. out |= TIOCM_CD;
  241. return out;
  242. }
  243. static int set_control_lines(struct ipw_tty *tty, unsigned int set,
  244. unsigned int clear)
  245. {
  246. int ret;
  247. if (set & TIOCM_RTS) {
  248. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
  249. if (ret)
  250. return ret;
  251. if (tty->secondary_channel_idx != -1) {
  252. ret = ipwireless_set_RTS(tty->hardware,
  253. tty->secondary_channel_idx, 1);
  254. if (ret)
  255. return ret;
  256. }
  257. }
  258. if (set & TIOCM_DTR) {
  259. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
  260. if (ret)
  261. return ret;
  262. if (tty->secondary_channel_idx != -1) {
  263. ret = ipwireless_set_DTR(tty->hardware,
  264. tty->secondary_channel_idx, 1);
  265. if (ret)
  266. return ret;
  267. }
  268. }
  269. if (clear & TIOCM_RTS) {
  270. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
  271. if (tty->secondary_channel_idx != -1) {
  272. ret = ipwireless_set_RTS(tty->hardware,
  273. tty->secondary_channel_idx, 0);
  274. if (ret)
  275. return ret;
  276. }
  277. }
  278. if (clear & TIOCM_DTR) {
  279. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
  280. if (tty->secondary_channel_idx != -1) {
  281. ret = ipwireless_set_DTR(tty->hardware,
  282. tty->secondary_channel_idx, 0);
  283. if (ret)
  284. return ret;
  285. }
  286. }
  287. return 0;
  288. }
  289. static int ipw_tiocmget(struct tty_struct *linux_tty)
  290. {
  291. struct ipw_tty *tty = linux_tty->driver_data;
  292. /* FIXME: Exactly how is the tty object locked here .. */
  293. if (!tty)
  294. return -ENODEV;
  295. if (!tty->port.count)
  296. return -EINVAL;
  297. return get_control_lines(tty);
  298. }
  299. static int
  300. ipw_tiocmset(struct tty_struct *linux_tty,
  301. unsigned int set, unsigned int clear)
  302. {
  303. struct ipw_tty *tty = linux_tty->driver_data;
  304. /* FIXME: Exactly how is the tty object locked here .. */
  305. if (!tty)
  306. return -ENODEV;
  307. if (!tty->port.count)
  308. return -EINVAL;
  309. return set_control_lines(tty, set, clear);
  310. }
  311. static int ipw_ioctl(struct tty_struct *linux_tty,
  312. unsigned int cmd, unsigned long arg)
  313. {
  314. struct ipw_tty *tty = linux_tty->driver_data;
  315. if (!tty)
  316. return -ENODEV;
  317. if (!tty->port.count)
  318. return -EINVAL;
  319. /* FIXME: Exactly how is the tty object locked here .. */
  320. if (tty->tty_type == TTYTYPE_MODEM) {
  321. switch (cmd) {
  322. case PPPIOCGCHAN:
  323. {
  324. int chan = ipwireless_ppp_channel_index(
  325. tty->network);
  326. if (chan < 0)
  327. return -ENODEV;
  328. if (put_user(chan, (int __user *) arg))
  329. return -EFAULT;
  330. }
  331. return 0;
  332. case PPPIOCGUNIT:
  333. {
  334. int unit = ipwireless_ppp_unit_number(
  335. tty->network);
  336. if (unit < 0)
  337. return -ENODEV;
  338. if (put_user(unit, (int __user *) arg))
  339. return -EFAULT;
  340. }
  341. return 0;
  342. case FIONREAD:
  343. {
  344. int val = 0;
  345. if (put_user(val, (int __user *) arg))
  346. return -EFAULT;
  347. }
  348. return 0;
  349. case TCFLSH:
  350. return tty_perform_flush(linux_tty, arg);
  351. }
  352. }
  353. return -ENOIOCTLCMD;
  354. }
  355. static int add_tty(int j,
  356. struct ipw_hardware *hardware,
  357. struct ipw_network *network, int channel_idx,
  358. int secondary_channel_idx, int tty_type)
  359. {
  360. ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
  361. if (!ttys[j])
  362. return -ENOMEM;
  363. ttys[j]->index = j;
  364. ttys[j]->hardware = hardware;
  365. ttys[j]->channel_idx = channel_idx;
  366. ttys[j]->secondary_channel_idx = secondary_channel_idx;
  367. ttys[j]->network = network;
  368. ttys[j]->tty_type = tty_type;
  369. mutex_init(&ttys[j]->ipw_tty_mutex);
  370. tty_port_init(&ttys[j]->port);
  371. tty_port_register_device(&ttys[j]->port, ipw_tty_driver, j, NULL);
  372. ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
  373. if (secondary_channel_idx != -1)
  374. ipwireless_associate_network_tty(network,
  375. secondary_channel_idx,
  376. ttys[j]);
  377. /* check if we provide raw device (if loopback is enabled) */
  378. if (get_tty(j))
  379. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  380. ": registering %s device ttyIPWp%d\n",
  381. tty_type_name(tty_type), j);
  382. return 0;
  383. }
  384. struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
  385. struct ipw_network *network)
  386. {
  387. int i, j;
  388. for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
  389. int allfree = 1;
  390. for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
  391. j += IPWIRELESS_PCMCIA_MINOR_RANGE)
  392. if (ttys[j] != NULL) {
  393. allfree = 0;
  394. break;
  395. }
  396. if (allfree) {
  397. j = i;
  398. if (add_tty(j, hardware, network,
  399. IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
  400. TTYTYPE_MODEM))
  401. return NULL;
  402. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  403. if (add_tty(j, hardware, network,
  404. IPW_CHANNEL_DIALLER, -1,
  405. TTYTYPE_MONITOR))
  406. return NULL;
  407. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  408. if (add_tty(j, hardware, network,
  409. IPW_CHANNEL_RAS, -1,
  410. TTYTYPE_RAS_RAW))
  411. return NULL;
  412. return ttys[i];
  413. }
  414. }
  415. return NULL;
  416. }
  417. /*
  418. * Must be called before ipwireless_network_free().
  419. */
  420. void ipwireless_tty_free(struct ipw_tty *tty)
  421. {
  422. int j;
  423. struct ipw_network *network = ttys[tty->index]->network;
  424. for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
  425. j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
  426. struct ipw_tty *ttyj = ttys[j];
  427. if (ttyj) {
  428. mutex_lock(&ttyj->ipw_tty_mutex);
  429. if (get_tty(j))
  430. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  431. ": deregistering %s device ttyIPWp%d\n",
  432. tty_type_name(ttyj->tty_type), j);
  433. ttyj->closing = 1;
  434. if (ttyj->port.tty != NULL) {
  435. mutex_unlock(&ttyj->ipw_tty_mutex);
  436. tty_vhangup(ttyj->port.tty);
  437. /* FIXME: Exactly how is the tty object locked here
  438. against a parallel ioctl etc */
  439. /* FIXME2: hangup does not mean all processes
  440. * are gone */
  441. mutex_lock(&ttyj->ipw_tty_mutex);
  442. }
  443. while (ttyj->port.count)
  444. do_ipw_close(ttyj);
  445. ipwireless_disassociate_network_ttys(network,
  446. ttyj->channel_idx);
  447. tty_unregister_device(ipw_tty_driver, j);
  448. tty_port_destroy(&ttyj->port);
  449. ttys[j] = NULL;
  450. mutex_unlock(&ttyj->ipw_tty_mutex);
  451. kfree(ttyj);
  452. }
  453. }
  454. }
  455. static const struct tty_operations tty_ops = {
  456. .open = ipw_open,
  457. .close = ipw_close,
  458. .hangup = ipw_hangup,
  459. .write = ipw_write,
  460. .write_room = ipw_write_room,
  461. .ioctl = ipw_ioctl,
  462. .chars_in_buffer = ipw_chars_in_buffer,
  463. .tiocmget = ipw_tiocmget,
  464. .tiocmset = ipw_tiocmset,
  465. .set_serial = ipwireless_set_serial_info,
  466. .get_serial = ipwireless_get_serial_info,
  467. };
  468. int ipwireless_tty_init(void)
  469. {
  470. int result;
  471. ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
  472. if (!ipw_tty_driver)
  473. return -ENOMEM;
  474. ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
  475. ipw_tty_driver->name = "ttyIPWp";
  476. ipw_tty_driver->major = 0;
  477. ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
  478. ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  479. ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  480. ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  481. ipw_tty_driver->init_termios = tty_std_termios;
  482. ipw_tty_driver->init_termios.c_cflag =
  483. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  484. ipw_tty_driver->init_termios.c_ispeed = 9600;
  485. ipw_tty_driver->init_termios.c_ospeed = 9600;
  486. tty_set_operations(ipw_tty_driver, &tty_ops);
  487. result = tty_register_driver(ipw_tty_driver);
  488. if (result) {
  489. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  490. ": failed to register tty driver\n");
  491. put_tty_driver(ipw_tty_driver);
  492. return result;
  493. }
  494. return 0;
  495. }
  496. void ipwireless_tty_release(void)
  497. {
  498. int ret;
  499. ret = tty_unregister_driver(ipw_tty_driver);
  500. put_tty_driver(ipw_tty_driver);
  501. if (ret != 0)
  502. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  503. ": tty_unregister_driver failed with code %d\n", ret);
  504. }
  505. int ipwireless_tty_is_modem(struct ipw_tty *tty)
  506. {
  507. return tty->tty_type == TTYTYPE_MODEM;
  508. }
  509. void
  510. ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
  511. unsigned int channel_idx,
  512. unsigned int control_lines,
  513. unsigned int changed_mask)
  514. {
  515. unsigned int old_control_lines = tty->control_lines;
  516. tty->control_lines = (tty->control_lines & ~changed_mask)
  517. | (control_lines & changed_mask);
  518. /*
  519. * If DCD is de-asserted, we close the tty so pppd can tell that we
  520. * have gone offline.
  521. */
  522. if ((old_control_lines & IPW_CONTROL_LINE_DCD)
  523. && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
  524. && tty->port.tty) {
  525. tty_hangup(tty->port.tty);
  526. }
  527. }