tty.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 ipw_tty *tty,
  201. struct serial_struct __user *retinfo)
  202. {
  203. struct serial_struct tmp;
  204. memset(&tmp, 0, sizeof(tmp));
  205. tmp.type = PORT_UNKNOWN;
  206. tmp.line = tty->index;
  207. tmp.baud_base = 115200;
  208. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  209. return -EFAULT;
  210. return 0;
  211. }
  212. static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
  213. {
  214. struct ipw_tty *tty = linux_tty->driver_data;
  215. if (!tty)
  216. return 0;
  217. if (!tty->port.count)
  218. return 0;
  219. return tty->tx_bytes_queued;
  220. }
  221. static int get_control_lines(struct ipw_tty *tty)
  222. {
  223. unsigned int my = tty->control_lines;
  224. unsigned int out = 0;
  225. if (my & IPW_CONTROL_LINE_RTS)
  226. out |= TIOCM_RTS;
  227. if (my & IPW_CONTROL_LINE_DTR)
  228. out |= TIOCM_DTR;
  229. if (my & IPW_CONTROL_LINE_CTS)
  230. out |= TIOCM_CTS;
  231. if (my & IPW_CONTROL_LINE_DSR)
  232. out |= TIOCM_DSR;
  233. if (my & IPW_CONTROL_LINE_DCD)
  234. out |= TIOCM_CD;
  235. return out;
  236. }
  237. static int set_control_lines(struct ipw_tty *tty, unsigned int set,
  238. unsigned int clear)
  239. {
  240. int ret;
  241. if (set & TIOCM_RTS) {
  242. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
  243. if (ret)
  244. return ret;
  245. if (tty->secondary_channel_idx != -1) {
  246. ret = ipwireless_set_RTS(tty->hardware,
  247. tty->secondary_channel_idx, 1);
  248. if (ret)
  249. return ret;
  250. }
  251. }
  252. if (set & TIOCM_DTR) {
  253. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
  254. if (ret)
  255. return ret;
  256. if (tty->secondary_channel_idx != -1) {
  257. ret = ipwireless_set_DTR(tty->hardware,
  258. tty->secondary_channel_idx, 1);
  259. if (ret)
  260. return ret;
  261. }
  262. }
  263. if (clear & TIOCM_RTS) {
  264. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
  265. if (tty->secondary_channel_idx != -1) {
  266. ret = ipwireless_set_RTS(tty->hardware,
  267. tty->secondary_channel_idx, 0);
  268. if (ret)
  269. return ret;
  270. }
  271. }
  272. if (clear & TIOCM_DTR) {
  273. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
  274. if (tty->secondary_channel_idx != -1) {
  275. ret = ipwireless_set_DTR(tty->hardware,
  276. tty->secondary_channel_idx, 0);
  277. if (ret)
  278. return ret;
  279. }
  280. }
  281. return 0;
  282. }
  283. static int ipw_tiocmget(struct tty_struct *linux_tty)
  284. {
  285. struct ipw_tty *tty = linux_tty->driver_data;
  286. /* FIXME: Exactly how is the tty object locked here .. */
  287. if (!tty)
  288. return -ENODEV;
  289. if (!tty->port.count)
  290. return -EINVAL;
  291. return get_control_lines(tty);
  292. }
  293. static int
  294. ipw_tiocmset(struct tty_struct *linux_tty,
  295. unsigned int set, unsigned int clear)
  296. {
  297. struct ipw_tty *tty = linux_tty->driver_data;
  298. /* FIXME: Exactly how is the tty object locked here .. */
  299. if (!tty)
  300. return -ENODEV;
  301. if (!tty->port.count)
  302. return -EINVAL;
  303. return set_control_lines(tty, set, clear);
  304. }
  305. static int ipw_ioctl(struct tty_struct *linux_tty,
  306. unsigned int cmd, unsigned long arg)
  307. {
  308. struct ipw_tty *tty = linux_tty->driver_data;
  309. if (!tty)
  310. return -ENODEV;
  311. if (!tty->port.count)
  312. return -EINVAL;
  313. /* FIXME: Exactly how is the tty object locked here .. */
  314. switch (cmd) {
  315. case TIOCGSERIAL:
  316. return ipwireless_get_serial_info(tty, (void __user *) arg);
  317. case TIOCSSERIAL:
  318. return 0; /* Keeps the PCMCIA scripts happy. */
  319. }
  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. };
  466. int ipwireless_tty_init(void)
  467. {
  468. int result;
  469. ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
  470. if (!ipw_tty_driver)
  471. return -ENOMEM;
  472. ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
  473. ipw_tty_driver->name = "ttyIPWp";
  474. ipw_tty_driver->major = 0;
  475. ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
  476. ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  477. ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  478. ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  479. ipw_tty_driver->init_termios = tty_std_termios;
  480. ipw_tty_driver->init_termios.c_cflag =
  481. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  482. ipw_tty_driver->init_termios.c_ispeed = 9600;
  483. ipw_tty_driver->init_termios.c_ospeed = 9600;
  484. tty_set_operations(ipw_tty_driver, &tty_ops);
  485. result = tty_register_driver(ipw_tty_driver);
  486. if (result) {
  487. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  488. ": failed to register tty driver\n");
  489. put_tty_driver(ipw_tty_driver);
  490. return result;
  491. }
  492. return 0;
  493. }
  494. void ipwireless_tty_release(void)
  495. {
  496. int ret;
  497. ret = tty_unregister_driver(ipw_tty_driver);
  498. put_tty_driver(ipw_tty_driver);
  499. if (ret != 0)
  500. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  501. ": tty_unregister_driver failed with code %d\n", ret);
  502. }
  503. int ipwireless_tty_is_modem(struct ipw_tty *tty)
  504. {
  505. return tty->tty_type == TTYTYPE_MODEM;
  506. }
  507. void
  508. ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
  509. unsigned int channel_idx,
  510. unsigned int control_lines,
  511. unsigned int changed_mask)
  512. {
  513. unsigned int old_control_lines = tty->control_lines;
  514. tty->control_lines = (tty->control_lines & ~changed_mask)
  515. | (control_lines & changed_mask);
  516. /*
  517. * If DCD is de-asserted, we close the tty so pppd can tell that we
  518. * have gone offline.
  519. */
  520. if ((old_control_lines & IPW_CONTROL_LINE_DCD)
  521. && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
  522. && tty->port.tty) {
  523. tty_hangup(tty->port.tty);
  524. }
  525. }