tty.c 14 KB

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