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