tty.c 14 KB

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