tty.c 14 KB

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