tty.c 14 KB

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