tty.c 14 KB

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