tty.c 14 KB

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