tty.c 14 KB

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