tty.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 <linux/version.h>
  32. #include "tty.h"
  33. #include "network.h"
  34. #include "hardware.h"
  35. #include "main.h"
  36. #define IPWIRELESS_PCMCIA_START (0)
  37. #define IPWIRELESS_PCMCIA_MINORS (24)
  38. #define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
  39. #define TTYTYPE_MODEM (0)
  40. #define TTYTYPE_MONITOR (1)
  41. #define TTYTYPE_RAS_RAW (2)
  42. struct ipw_tty {
  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. int open_count;
  51. unsigned int control_lines;
  52. struct mutex ipw_tty_mutex;
  53. int tx_bytes_queued;
  54. int closing;
  55. };
  56. static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
  57. static struct tty_driver *ipw_tty_driver;
  58. static char *tty_type_name(int tty_type)
  59. {
  60. static char *channel_names[] = {
  61. "modem",
  62. "monitor",
  63. "RAS-raw"
  64. };
  65. return channel_names[tty_type];
  66. }
  67. static void report_registering(struct ipw_tty *tty)
  68. {
  69. char *iftype = tty_type_name(tty->tty_type);
  70. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  71. ": registering %s device ttyIPWp%d\n", iftype, tty->index);
  72. }
  73. static void report_deregistering(struct ipw_tty *tty)
  74. {
  75. char *iftype = tty_type_name(tty->tty_type);
  76. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  77. ": deregistering %s device ttyIPWp%d\n", iftype,
  78. tty->index);
  79. }
  80. static struct ipw_tty *get_tty(int minor)
  81. {
  82. if (minor < ipw_tty_driver->minor_start
  83. || minor >= ipw_tty_driver->minor_start +
  84. IPWIRELESS_PCMCIA_MINORS)
  85. return NULL;
  86. else {
  87. int minor_offset = minor - ipw_tty_driver->minor_start;
  88. /*
  89. * The 'ras_raw' channel is only available when 'loopback' mode
  90. * is enabled.
  91. * Number of minor starts with 16 (_RANGE * _RAS_RAW).
  92. */
  93. if (!ipwireless_loopback &&
  94. minor_offset >=
  95. IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
  96. return NULL;
  97. return ttys[minor_offset];
  98. }
  99. }
  100. static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
  101. {
  102. int minor = linux_tty->index;
  103. struct ipw_tty *tty = get_tty(minor);
  104. if (!tty)
  105. return -ENODEV;
  106. mutex_lock(&tty->ipw_tty_mutex);
  107. if (tty->closing) {
  108. mutex_unlock(&tty->ipw_tty_mutex);
  109. return -ENODEV;
  110. }
  111. if (tty->open_count == 0)
  112. tty->tx_bytes_queued = 0;
  113. tty->open_count++;
  114. tty->linux_tty = linux_tty;
  115. linux_tty->driver_data = tty;
  116. linux_tty->low_latency = 1;
  117. if (tty->tty_type == TTYTYPE_MODEM)
  118. ipwireless_ppp_open(tty->network);
  119. mutex_unlock(&tty->ipw_tty_mutex);
  120. return 0;
  121. }
  122. static void do_ipw_close(struct ipw_tty *tty)
  123. {
  124. tty->open_count--;
  125. if (tty->open_count == 0) {
  126. struct tty_struct *linux_tty = tty->linux_tty;
  127. if (linux_tty != NULL) {
  128. tty->linux_tty = NULL;
  129. linux_tty->driver_data = NULL;
  130. if (tty->tty_type == TTYTYPE_MODEM)
  131. ipwireless_ppp_close(tty->network);
  132. }
  133. }
  134. }
  135. static void ipw_hangup(struct tty_struct *linux_tty)
  136. {
  137. struct ipw_tty *tty = linux_tty->driver_data;
  138. if (!tty)
  139. return;
  140. mutex_lock(&tty->ipw_tty_mutex);
  141. if (tty->open_count == 0) {
  142. mutex_unlock(&tty->ipw_tty_mutex);
  143. return;
  144. }
  145. do_ipw_close(tty);
  146. mutex_unlock(&tty->ipw_tty_mutex);
  147. }
  148. static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
  149. {
  150. ipw_hangup(linux_tty);
  151. }
  152. /* Take data received from hardware, and send it out the tty */
  153. void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
  154. unsigned int length)
  155. {
  156. struct tty_struct *linux_tty;
  157. int work = 0;
  158. mutex_lock(&tty->ipw_tty_mutex);
  159. linux_tty = tty->linux_tty;
  160. if (linux_tty == NULL) {
  161. mutex_unlock(&tty->ipw_tty_mutex);
  162. return;
  163. }
  164. if (!tty->open_count) {
  165. mutex_unlock(&tty->ipw_tty_mutex);
  166. return;
  167. }
  168. mutex_unlock(&tty->ipw_tty_mutex);
  169. work = tty_insert_flip_string(linux_tty, data, length);
  170. if (work != length)
  171. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  172. ": %d chars not inserted to flip buffer!\n",
  173. length - work);
  174. /*
  175. * This may sleep if ->low_latency is set
  176. */
  177. if (work)
  178. tty_flip_buffer_push(linux_tty);
  179. }
  180. static void ipw_write_packet_sent_callback(void *callback_data,
  181. unsigned int packet_length)
  182. {
  183. struct ipw_tty *tty = callback_data;
  184. /*
  185. * Packet has been sent, so we subtract the number of bytes from our
  186. * tally of outstanding TX bytes.
  187. */
  188. tty->tx_bytes_queued -= packet_length;
  189. }
  190. static int ipw_write(struct tty_struct *linux_tty,
  191. const unsigned char *buf, int count)
  192. {
  193. struct ipw_tty *tty = linux_tty->driver_data;
  194. int room, ret;
  195. if (!tty)
  196. return -ENODEV;
  197. mutex_lock(&tty->ipw_tty_mutex);
  198. if (!tty->open_count) {
  199. mutex_unlock(&tty->ipw_tty_mutex);
  200. return -EINVAL;
  201. }
  202. room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
  203. if (room < 0)
  204. room = 0;
  205. /* Don't allow caller to write any more than we have room for */
  206. if (count > room)
  207. count = room;
  208. if (count == 0) {
  209. mutex_unlock(&tty->ipw_tty_mutex);
  210. return 0;
  211. }
  212. ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
  213. (unsigned char *) buf, count,
  214. ipw_write_packet_sent_callback, tty);
  215. if (ret == -1) {
  216. mutex_unlock(&tty->ipw_tty_mutex);
  217. return 0;
  218. }
  219. tty->tx_bytes_queued += count;
  220. mutex_unlock(&tty->ipw_tty_mutex);
  221. return count;
  222. }
  223. static int ipw_write_room(struct tty_struct *linux_tty)
  224. {
  225. struct ipw_tty *tty = linux_tty->driver_data;
  226. int room;
  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 -ENODEV;
  262. if (!tty->open_count)
  263. return -EINVAL;
  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, struct file *file)
  329. {
  330. struct ipw_tty *tty = linux_tty->driver_data;
  331. if (!tty)
  332. return -ENODEV;
  333. if (!tty->open_count)
  334. return -EINVAL;
  335. return get_control_lines(tty);
  336. }
  337. static int
  338. ipw_tiocmset(struct tty_struct *linux_tty, struct file *file,
  339. unsigned int set, unsigned int clear)
  340. {
  341. struct ipw_tty *tty = linux_tty->driver_data;
  342. if (!tty)
  343. return -ENODEV;
  344. if (!tty->open_count)
  345. return -EINVAL;
  346. return set_control_lines(tty, set, clear);
  347. }
  348. static int ipw_ioctl(struct tty_struct *linux_tty, struct file *file,
  349. unsigned int cmd, unsigned long arg)
  350. {
  351. struct ipw_tty *tty = linux_tty->driver_data;
  352. if (!tty)
  353. return -ENODEV;
  354. if (!tty->open_count)
  355. return -EINVAL;
  356. switch (cmd) {
  357. case TIOCGSERIAL:
  358. return ipwireless_get_serial_info(tty, (void __user *) arg);
  359. case TIOCSSERIAL:
  360. return 0; /* Keeps the PCMCIA scripts happy. */
  361. }
  362. if (tty->tty_type == TTYTYPE_MODEM) {
  363. switch (cmd) {
  364. case PPPIOCGCHAN:
  365. {
  366. int chan = ipwireless_ppp_channel_index(
  367. tty->network);
  368. if (chan < 0)
  369. return -ENODEV;
  370. if (put_user(chan, (int __user *) arg))
  371. return -EFAULT;
  372. }
  373. return 0;
  374. case PPPIOCGUNIT:
  375. {
  376. int unit = ipwireless_ppp_unit_number(
  377. tty->network);
  378. if (unit < 0)
  379. return -ENODEV;
  380. if (put_user(unit, (int __user *) arg))
  381. return -EFAULT;
  382. }
  383. return 0;
  384. case TCGETS:
  385. case TCGETA:
  386. return n_tty_ioctl(linux_tty, file, cmd, arg);
  387. case TCFLSH:
  388. return n_tty_ioctl(linux_tty, file, cmd, arg);
  389. case FIONREAD:
  390. {
  391. int val = 0;
  392. if (put_user(val, (int __user *) arg))
  393. return -EFAULT;
  394. }
  395. return 0;
  396. }
  397. }
  398. return -ENOIOCTLCMD;
  399. }
  400. static int add_tty(dev_node_t *nodesp, 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 (nodesp != NULL) {
  422. sprintf(nodesp->dev_name, "ttyIPWp%d", j);
  423. nodesp->major = ipw_tty_driver->major;
  424. nodesp->minor = j + ipw_tty_driver->minor_start;
  425. }
  426. if (get_tty(j + ipw_tty_driver->minor_start) == ttys[j])
  427. report_registering(ttys[j]);
  428. return 0;
  429. }
  430. struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
  431. struct ipw_network *network,
  432. dev_node_t *nodes)
  433. {
  434. int i, j;
  435. for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
  436. int allfree = 1;
  437. for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
  438. j += IPWIRELESS_PCMCIA_MINOR_RANGE)
  439. if (ttys[j] != NULL) {
  440. allfree = 0;
  441. break;
  442. }
  443. if (allfree) {
  444. j = i;
  445. if (add_tty(&nodes[0], j, hardware, network,
  446. IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
  447. TTYTYPE_MODEM))
  448. return NULL;
  449. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  450. if (add_tty(&nodes[1], j, hardware, network,
  451. IPW_CHANNEL_DIALLER, -1,
  452. TTYTYPE_MONITOR))
  453. return NULL;
  454. j += IPWIRELESS_PCMCIA_MINOR_RANGE;
  455. if (add_tty(NULL, j, hardware, network,
  456. IPW_CHANNEL_RAS, -1,
  457. TTYTYPE_RAS_RAW))
  458. return NULL;
  459. nodes[0].next = &nodes[1];
  460. nodes[1].next = NULL;
  461. return ttys[i];
  462. }
  463. }
  464. return NULL;
  465. }
  466. /*
  467. * Must be called before ipwireless_network_free().
  468. */
  469. void ipwireless_tty_free(struct ipw_tty *tty)
  470. {
  471. int j;
  472. struct ipw_network *network = ttys[tty->index]->network;
  473. for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
  474. j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
  475. struct ipw_tty *ttyj = ttys[j];
  476. if (ttyj) {
  477. mutex_lock(&ttyj->ipw_tty_mutex);
  478. if (get_tty(j + ipw_tty_driver->minor_start) == ttyj)
  479. report_deregistering(ttyj);
  480. ttyj->closing = 1;
  481. if (ttyj->linux_tty != NULL) {
  482. mutex_unlock(&ttyj->ipw_tty_mutex);
  483. tty_hangup(ttyj->linux_tty);
  484. /* Wait till the tty_hangup has completed */
  485. flush_scheduled_work();
  486. mutex_lock(&ttyj->ipw_tty_mutex);
  487. }
  488. while (ttyj->open_count)
  489. do_ipw_close(ttyj);
  490. ipwireless_disassociate_network_ttys(network,
  491. ttyj->channel_idx);
  492. tty_unregister_device(ipw_tty_driver, j);
  493. ttys[j] = NULL;
  494. mutex_unlock(&ttyj->ipw_tty_mutex);
  495. kfree(ttyj);
  496. }
  497. }
  498. }
  499. static struct tty_operations tty_ops = {
  500. .open = ipw_open,
  501. .close = ipw_close,
  502. .hangup = ipw_hangup,
  503. .write = ipw_write,
  504. .write_room = ipw_write_room,
  505. .ioctl = ipw_ioctl,
  506. .chars_in_buffer = ipw_chars_in_buffer,
  507. .tiocmget = ipw_tiocmget,
  508. .tiocmset = ipw_tiocmset,
  509. };
  510. int ipwireless_tty_init(void)
  511. {
  512. int result;
  513. ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
  514. if (!ipw_tty_driver)
  515. return -ENOMEM;
  516. ipw_tty_driver->owner = THIS_MODULE;
  517. ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
  518. ipw_tty_driver->name = "ttyIPWp";
  519. ipw_tty_driver->major = 0;
  520. ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
  521. ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  522. ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  523. ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  524. ipw_tty_driver->init_termios = tty_std_termios;
  525. ipw_tty_driver->init_termios.c_cflag =
  526. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  527. ipw_tty_driver->init_termios.c_ispeed = 9600;
  528. ipw_tty_driver->init_termios.c_ospeed = 9600;
  529. tty_set_operations(ipw_tty_driver, &tty_ops);
  530. result = tty_register_driver(ipw_tty_driver);
  531. if (result) {
  532. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  533. ": failed to register tty driver\n");
  534. put_tty_driver(ipw_tty_driver);
  535. return result;
  536. }
  537. return 0;
  538. }
  539. void ipwireless_tty_release(void)
  540. {
  541. int ret;
  542. ret = tty_unregister_driver(ipw_tty_driver);
  543. put_tty_driver(ipw_tty_driver);
  544. if (ret != 0)
  545. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  546. ": tty_unregister_driver failed with code %d\n", ret);
  547. }
  548. int ipwireless_tty_is_modem(struct ipw_tty *tty)
  549. {
  550. return tty->tty_type == TTYTYPE_MODEM;
  551. }
  552. void
  553. ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
  554. unsigned int channel_idx,
  555. unsigned int control_lines,
  556. unsigned int changed_mask)
  557. {
  558. unsigned int old_control_lines = tty->control_lines;
  559. tty->control_lines = (tty->control_lines & ~changed_mask)
  560. | (control_lines & changed_mask);
  561. /*
  562. * If DCD is de-asserted, we close the tty so pppd can tell that we
  563. * have gone offline.
  564. */
  565. if ((old_control_lines & IPW_CONTROL_LINE_DCD)
  566. && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
  567. && tty->linux_tty) {
  568. tty_hangup(tty->linux_tty);
  569. }
  570. }