tty.c 14 KB

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