tty.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. if (!tty)
  227. return -ENODEV;
  228. if (!tty->open_count)
  229. return -EINVAL;
  230. room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
  231. if (room < 0)
  232. room = 0;
  233. return room;
  234. }
  235. static int ipwireless_get_serial_info(struct ipw_tty *tty,
  236. struct serial_struct __user *retinfo)
  237. {
  238. struct serial_struct tmp;
  239. if (!retinfo)
  240. return (-EFAULT);
  241. memset(&tmp, 0, sizeof(tmp));
  242. tmp.type = PORT_UNKNOWN;
  243. tmp.line = tty->index;
  244. tmp.port = 0;
  245. tmp.irq = 0;
  246. tmp.flags = 0;
  247. tmp.baud_base = 115200;
  248. tmp.close_delay = 0;
  249. tmp.closing_wait = 0;
  250. tmp.custom_divisor = 0;
  251. tmp.hub6 = 0;
  252. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  253. return -EFAULT;
  254. return 0;
  255. }
  256. static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
  257. {
  258. struct ipw_tty *tty = linux_tty->driver_data;
  259. if (!tty)
  260. return -ENODEV;
  261. if (!tty->open_count)
  262. return -EINVAL;
  263. return tty->tx_bytes_queued;
  264. }
  265. static int get_control_lines(struct ipw_tty *tty)
  266. {
  267. unsigned int my = tty->control_lines;
  268. unsigned int out = 0;
  269. if (my & IPW_CONTROL_LINE_RTS)
  270. out |= TIOCM_RTS;
  271. if (my & IPW_CONTROL_LINE_DTR)
  272. out |= TIOCM_DTR;
  273. if (my & IPW_CONTROL_LINE_CTS)
  274. out |= TIOCM_CTS;
  275. if (my & IPW_CONTROL_LINE_DSR)
  276. out |= TIOCM_DSR;
  277. if (my & IPW_CONTROL_LINE_DCD)
  278. out |= TIOCM_CD;
  279. return out;
  280. }
  281. static int set_control_lines(struct ipw_tty *tty, unsigned int set,
  282. unsigned int clear)
  283. {
  284. int ret;
  285. if (set & TIOCM_RTS) {
  286. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
  287. if (ret)
  288. return ret;
  289. if (tty->secondary_channel_idx != -1) {
  290. ret = ipwireless_set_RTS(tty->hardware,
  291. tty->secondary_channel_idx, 1);
  292. if (ret)
  293. return ret;
  294. }
  295. }
  296. if (set & TIOCM_DTR) {
  297. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
  298. if (ret)
  299. return ret;
  300. if (tty->secondary_channel_idx != -1) {
  301. ret = ipwireless_set_DTR(tty->hardware,
  302. tty->secondary_channel_idx, 1);
  303. if (ret)
  304. return ret;
  305. }
  306. }
  307. if (clear & TIOCM_RTS) {
  308. ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
  309. if (tty->secondary_channel_idx != -1) {
  310. ret = ipwireless_set_RTS(tty->hardware,
  311. tty->secondary_channel_idx, 0);
  312. if (ret)
  313. return ret;
  314. }
  315. }
  316. if (clear & TIOCM_DTR) {
  317. ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
  318. if (tty->secondary_channel_idx != -1) {
  319. ret = ipwireless_set_DTR(tty->hardware,
  320. tty->secondary_channel_idx, 0);
  321. if (ret)
  322. return ret;
  323. }
  324. }
  325. return 0;
  326. }
  327. static int ipw_tiocmget(struct tty_struct *linux_tty, struct file *file)
  328. {
  329. struct ipw_tty *tty = linux_tty->driver_data;
  330. if (!tty)
  331. return -ENODEV;
  332. if (!tty->open_count)
  333. return -EINVAL;
  334. return get_control_lines(tty);
  335. }
  336. static int
  337. ipw_tiocmset(struct tty_struct *linux_tty, struct file *file,
  338. unsigned int set, unsigned int clear)
  339. {
  340. struct ipw_tty *tty = linux_tty->driver_data;
  341. if (!tty)
  342. return -ENODEV;
  343. if (!tty->open_count)
  344. return -EINVAL;
  345. return set_control_lines(tty, set, clear);
  346. }
  347. static int ipw_ioctl(struct tty_struct *linux_tty, struct file *file,
  348. unsigned int cmd, unsigned long arg)
  349. {
  350. struct ipw_tty *tty = linux_tty->driver_data;
  351. if (!tty)
  352. return -ENODEV;
  353. if (!tty->open_count)
  354. return -EINVAL;
  355. switch (cmd) {
  356. case TIOCGSERIAL:
  357. return ipwireless_get_serial_info(tty, (void __user *) arg);
  358. case TIOCSSERIAL:
  359. return 0; /* Keeps the PCMCIA scripts happy. */
  360. }
  361. if (tty->tty_type == TTYTYPE_MODEM) {
  362. switch (cmd) {
  363. case PPPIOCGCHAN:
  364. {
  365. int chan = ipwireless_ppp_channel_index(
  366. tty->network);
  367. if (chan < 0)
  368. return -ENODEV;
  369. if (put_user(chan, (int __user *) arg))
  370. return -EFAULT;
  371. }
  372. return 0;
  373. case PPPIOCGUNIT:
  374. {
  375. int unit = ipwireless_ppp_unit_number(
  376. tty->network);
  377. if (unit < 0)
  378. return -ENODEV;
  379. if (put_user(unit, (int __user *) arg))
  380. return -EFAULT;
  381. }
  382. return 0;
  383. case TCGETS:
  384. case TCGETA:
  385. return n_tty_ioctl(linux_tty, file, cmd, arg);
  386. case TCFLSH:
  387. return n_tty_ioctl(linux_tty, file, cmd, arg);
  388. case FIONREAD:
  389. {
  390. int val = 0;
  391. if (put_user(val, (int __user *) arg))
  392. return -EFAULT;
  393. }
  394. return 0;
  395. }
  396. }
  397. return -ENOIOCTLCMD;
  398. }
  399. static int add_tty(dev_node_t *nodesp, int j,
  400. struct ipw_hardware *hardware,
  401. struct ipw_network *network, int channel_idx,
  402. int secondary_channel_idx, int tty_type)
  403. {
  404. ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
  405. if (!ttys[j])
  406. return -ENOMEM;
  407. ttys[j]->index = j;
  408. ttys[j]->hardware = hardware;
  409. ttys[j]->channel_idx = channel_idx;
  410. ttys[j]->secondary_channel_idx = secondary_channel_idx;
  411. ttys[j]->network = network;
  412. ttys[j]->tty_type = tty_type;
  413. mutex_init(&ttys[j]->ipw_tty_mutex);
  414. tty_register_device(ipw_tty_driver, j, NULL);
  415. ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
  416. if (secondary_channel_idx != -1)
  417. ipwireless_associate_network_tty(network,
  418. secondary_channel_idx,
  419. ttys[j]);
  420. if (nodesp != NULL) {
  421. sprintf(nodesp->dev_name, "ttyIPWp%d", j);
  422. nodesp->major = ipw_tty_driver->major;
  423. nodesp->minor = j + ipw_tty_driver->minor_start;
  424. }
  425. if (get_tty(j + ipw_tty_driver->minor_start) == ttys[j])
  426. report_registering(ttys[j]);
  427. return 0;
  428. }
  429. struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
  430. struct ipw_network *network,
  431. dev_node_t *nodes)
  432. {
  433. int i, j;
  434. for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
  435. int allfree = 1;
  436. for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
  437. j += IPWIRELESS_PCMCIA_MINOR_RANGE)
  438. if (ttys[j] != NULL) {
  439. allfree = 0;
  440. break;
  441. }
  442. if (allfree) {
  443. j = i;
  444. if (add_tty(&nodes[0], j, hardware, network,
  445. IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
  446. TTYTYPE_MODEM))
  447. return NULL;
  448. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  449. if (add_tty(&nodes[1], j, hardware, network,
  450. IPW_CHANNEL_DIALLER, -1,
  451. TTYTYPE_MONITOR))
  452. return NULL;
  453. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  454. if (add_tty(NULL, j, hardware, network,
  455. IPW_CHANNEL_RAS, -1,
  456. TTYTYPE_RAS_RAW))
  457. return NULL;
  458. nodes[0].next = &nodes[1];
  459. nodes[1].next = NULL;
  460. return ttys[i];
  461. }
  462. }
  463. return NULL;
  464. }
  465. /*
  466. * Must be called before ipwireless_network_free().
  467. */
  468. void ipwireless_tty_free(struct ipw_tty *tty)
  469. {
  470. int j;
  471. struct ipw_network *network = ttys[tty->index]->network;
  472. for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
  473. j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
  474. struct ipw_tty *ttyj = ttys[j];
  475. if (ttyj) {
  476. mutex_lock(&ttyj->ipw_tty_mutex);
  477. if (get_tty(j + ipw_tty_driver->minor_start) == ttyj)
  478. report_deregistering(ttyj);
  479. ttyj->closing = 1;
  480. if (ttyj->linux_tty != NULL) {
  481. mutex_unlock(&ttyj->ipw_tty_mutex);
  482. tty_hangup(ttyj->linux_tty);
  483. /* Wait till the tty_hangup has completed */
  484. flush_scheduled_work();
  485. mutex_lock(&ttyj->ipw_tty_mutex);
  486. }
  487. while (ttyj->open_count)
  488. do_ipw_close(ttyj);
  489. ipwireless_disassociate_network_ttys(network,
  490. ttyj->channel_idx);
  491. tty_unregister_device(ipw_tty_driver, j);
  492. ttys[j] = NULL;
  493. mutex_unlock(&ttyj->ipw_tty_mutex);
  494. kfree(ttyj);
  495. }
  496. }
  497. }
  498. static struct tty_operations tty_ops = {
  499. .open = ipw_open,
  500. .close = ipw_close,
  501. .hangup = ipw_hangup,
  502. .write = ipw_write,
  503. .write_room = ipw_write_room,
  504. .ioctl = ipw_ioctl,
  505. .chars_in_buffer = ipw_chars_in_buffer,
  506. .tiocmget = ipw_tiocmget,
  507. .tiocmset = ipw_tiocmset,
  508. };
  509. int ipwireless_tty_init(void)
  510. {
  511. int result;
  512. ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
  513. if (!ipw_tty_driver)
  514. return -ENOMEM;
  515. ipw_tty_driver->owner = THIS_MODULE;
  516. ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
  517. ipw_tty_driver->name = "ttyIPWp";
  518. ipw_tty_driver->major = 0;
  519. ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
  520. ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  521. ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  522. ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  523. ipw_tty_driver->init_termios = tty_std_termios;
  524. ipw_tty_driver->init_termios.c_cflag =
  525. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  526. ipw_tty_driver->init_termios.c_ispeed = 9600;
  527. ipw_tty_driver->init_termios.c_ospeed = 9600;
  528. tty_set_operations(ipw_tty_driver, &tty_ops);
  529. result = tty_register_driver(ipw_tty_driver);
  530. if (result) {
  531. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  532. ": failed to register tty driver\n");
  533. put_tty_driver(ipw_tty_driver);
  534. return result;
  535. }
  536. return 0;
  537. }
  538. void ipwireless_tty_release(void)
  539. {
  540. int ret;
  541. ret = tty_unregister_driver(ipw_tty_driver);
  542. put_tty_driver(ipw_tty_driver);
  543. if (ret != 0)
  544. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  545. ": tty_unregister_driver failed with code %d\n", ret);
  546. }
  547. int ipwireless_tty_is_modem(struct ipw_tty *tty)
  548. {
  549. return tty->tty_type == TTYTYPE_MODEM;
  550. }
  551. void
  552. ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
  553. unsigned int channel_idx,
  554. unsigned int control_lines,
  555. unsigned int changed_mask)
  556. {
  557. unsigned int old_control_lines = tty->control_lines;
  558. tty->control_lines = (tty->control_lines & ~changed_mask)
  559. | (control_lines & changed_mask);
  560. /*
  561. * If DCD is de-asserted, we close the tty so pppd can tell that we
  562. * have gone offline.
  563. */
  564. if ((old_control_lines & IPW_CONTROL_LINE_DCD)
  565. && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
  566. && tty->linux_tty) {
  567. tty_hangup(tty->linux_tty);
  568. }
  569. }