tty.c 15 KB

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