interface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * interface to user space for the gigaset driver
  3. *
  4. * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
  5. *
  6. * =====================================================================
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. * =====================================================================
  12. */
  13. #include "gigaset.h"
  14. #include <linux/gigaset_dev.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/module.h>
  17. /*** our ioctls ***/
  18. static int if_lock(struct cardstate *cs, int *arg)
  19. {
  20. int cmd = *arg;
  21. gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
  22. if (cmd > 1)
  23. return -EINVAL;
  24. if (cmd < 0) {
  25. *arg = cs->mstate == MS_LOCKED;
  26. return 0;
  27. }
  28. if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
  29. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
  30. cs->ops->baud_rate(cs, B115200);
  31. cs->ops->set_line_ctrl(cs, CS8);
  32. cs->control_state = TIOCM_DTR | TIOCM_RTS;
  33. }
  34. cs->waiting = 1;
  35. if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
  36. NULL, cmd, NULL)) {
  37. cs->waiting = 0;
  38. return -ENOMEM;
  39. }
  40. gigaset_schedule_event(cs);
  41. wait_event(cs->waitqueue, !cs->waiting);
  42. if (cs->cmd_result >= 0) {
  43. *arg = cs->cmd_result;
  44. return 0;
  45. }
  46. return cs->cmd_result;
  47. }
  48. static int if_version(struct cardstate *cs, unsigned arg[4])
  49. {
  50. static const unsigned version[4] = GIG_VERSION;
  51. static const unsigned compat[4] = GIG_COMPAT;
  52. unsigned cmd = arg[0];
  53. gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
  54. switch (cmd) {
  55. case GIGVER_DRIVER:
  56. memcpy(arg, version, sizeof version);
  57. return 0;
  58. case GIGVER_COMPAT:
  59. memcpy(arg, compat, sizeof compat);
  60. return 0;
  61. case GIGVER_FWBASE:
  62. cs->waiting = 1;
  63. if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
  64. NULL, 0, arg)) {
  65. cs->waiting = 0;
  66. return -ENOMEM;
  67. }
  68. gigaset_schedule_event(cs);
  69. wait_event(cs->waitqueue, !cs->waiting);
  70. if (cs->cmd_result >= 0)
  71. return 0;
  72. return cs->cmd_result;
  73. default:
  74. return -EINVAL;
  75. }
  76. }
  77. static int if_config(struct cardstate *cs, int *arg)
  78. {
  79. gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
  80. if (*arg != 1)
  81. return -EINVAL;
  82. if (cs->mstate != MS_LOCKED)
  83. return -EBUSY;
  84. if (!cs->connected) {
  85. pr_err("%s: not connected\n", __func__);
  86. return -ENODEV;
  87. }
  88. *arg = 0;
  89. return gigaset_enterconfigmode(cs);
  90. }
  91. /*** the terminal driver ***/
  92. /* stolen from usbserial and some other tty drivers */
  93. static int if_open(struct tty_struct *tty, struct file *filp);
  94. static void if_close(struct tty_struct *tty, struct file *filp);
  95. static int if_ioctl(struct tty_struct *tty,
  96. unsigned int cmd, unsigned long arg);
  97. static int if_write_room(struct tty_struct *tty);
  98. static int if_chars_in_buffer(struct tty_struct *tty);
  99. static void if_throttle(struct tty_struct *tty);
  100. static void if_unthrottle(struct tty_struct *tty);
  101. static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
  102. static int if_tiocmget(struct tty_struct *tty);
  103. static int if_tiocmset(struct tty_struct *tty,
  104. unsigned int set, unsigned int clear);
  105. static int if_write(struct tty_struct *tty,
  106. const unsigned char *buf, int count);
  107. static const struct tty_operations if_ops = {
  108. .open = if_open,
  109. .close = if_close,
  110. .ioctl = if_ioctl,
  111. .write = if_write,
  112. .write_room = if_write_room,
  113. .chars_in_buffer = if_chars_in_buffer,
  114. .set_termios = if_set_termios,
  115. .throttle = if_throttle,
  116. .unthrottle = if_unthrottle,
  117. .tiocmget = if_tiocmget,
  118. .tiocmset = if_tiocmset,
  119. };
  120. static int if_open(struct tty_struct *tty, struct file *filp)
  121. {
  122. struct cardstate *cs;
  123. gig_dbg(DEBUG_IF, "%d+%d: %s()",
  124. tty->driver->minor_start, tty->index, __func__);
  125. cs = gigaset_get_cs_by_tty(tty);
  126. if (!cs || !try_module_get(cs->driver->owner))
  127. return -ENODEV;
  128. if (mutex_lock_interruptible(&cs->mutex)) {
  129. module_put(cs->driver->owner);
  130. return -ERESTARTSYS;
  131. }
  132. tty->driver_data = cs;
  133. ++cs->port.count;
  134. if (cs->port.count == 1) {
  135. tty_port_tty_set(&cs->port, tty);
  136. tty->low_latency = 1;
  137. }
  138. mutex_unlock(&cs->mutex);
  139. return 0;
  140. }
  141. static void if_close(struct tty_struct *tty, struct file *filp)
  142. {
  143. struct cardstate *cs = tty->driver_data;
  144. if (!cs) { /* happens if we didn't find cs in open */
  145. gig_dbg(DEBUG_IF, "%s: no cardstate", __func__);
  146. return;
  147. }
  148. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  149. mutex_lock(&cs->mutex);
  150. if (!cs->connected)
  151. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  152. else if (!cs->port.count)
  153. dev_warn(cs->dev, "%s: device not opened\n", __func__);
  154. else if (!--cs->port.count)
  155. tty_port_tty_set(&cs->port, NULL);
  156. mutex_unlock(&cs->mutex);
  157. module_put(cs->driver->owner);
  158. }
  159. static int if_ioctl(struct tty_struct *tty,
  160. unsigned int cmd, unsigned long arg)
  161. {
  162. struct cardstate *cs = tty->driver_data;
  163. int retval = -ENODEV;
  164. int int_arg;
  165. unsigned char buf[6];
  166. unsigned version[4];
  167. gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
  168. if (mutex_lock_interruptible(&cs->mutex))
  169. return -ERESTARTSYS;
  170. if (!cs->connected) {
  171. gig_dbg(DEBUG_IF, "not connected");
  172. retval = -ENODEV;
  173. } else {
  174. retval = 0;
  175. switch (cmd) {
  176. case GIGASET_REDIR:
  177. retval = get_user(int_arg, (int __user *) arg);
  178. if (retval >= 0)
  179. retval = if_lock(cs, &int_arg);
  180. if (retval >= 0)
  181. retval = put_user(int_arg, (int __user *) arg);
  182. break;
  183. case GIGASET_CONFIG:
  184. retval = get_user(int_arg, (int __user *) arg);
  185. if (retval >= 0)
  186. retval = if_config(cs, &int_arg);
  187. if (retval >= 0)
  188. retval = put_user(int_arg, (int __user *) arg);
  189. break;
  190. case GIGASET_BRKCHARS:
  191. retval = copy_from_user(&buf,
  192. (const unsigned char __user *) arg, 6)
  193. ? -EFAULT : 0;
  194. if (retval >= 0) {
  195. gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
  196. 6, (const unsigned char *) arg);
  197. retval = cs->ops->brkchars(cs, buf);
  198. }
  199. break;
  200. case GIGASET_VERSION:
  201. retval = copy_from_user(version,
  202. (unsigned __user *) arg, sizeof version)
  203. ? -EFAULT : 0;
  204. if (retval >= 0)
  205. retval = if_version(cs, version);
  206. if (retval >= 0)
  207. retval = copy_to_user((unsigned __user *) arg,
  208. version, sizeof version)
  209. ? -EFAULT : 0;
  210. break;
  211. default:
  212. gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
  213. __func__, cmd);
  214. retval = -ENOIOCTLCMD;
  215. }
  216. }
  217. mutex_unlock(&cs->mutex);
  218. return retval;
  219. }
  220. static int if_tiocmget(struct tty_struct *tty)
  221. {
  222. struct cardstate *cs = tty->driver_data;
  223. int retval;
  224. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  225. if (mutex_lock_interruptible(&cs->mutex))
  226. return -ERESTARTSYS;
  227. retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
  228. mutex_unlock(&cs->mutex);
  229. return retval;
  230. }
  231. static int if_tiocmset(struct tty_struct *tty,
  232. unsigned int set, unsigned int clear)
  233. {
  234. struct cardstate *cs = tty->driver_data;
  235. int retval;
  236. unsigned mc;
  237. gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
  238. cs->minor_index, __func__, set, clear);
  239. if (mutex_lock_interruptible(&cs->mutex))
  240. return -ERESTARTSYS;
  241. if (!cs->connected) {
  242. gig_dbg(DEBUG_IF, "not connected");
  243. retval = -ENODEV;
  244. } else {
  245. mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
  246. retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
  247. cs->control_state = mc;
  248. }
  249. mutex_unlock(&cs->mutex);
  250. return retval;
  251. }
  252. static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
  253. {
  254. struct cardstate *cs = tty->driver_data;
  255. struct cmdbuf_t *cb;
  256. int retval;
  257. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  258. if (mutex_lock_interruptible(&cs->mutex))
  259. return -ERESTARTSYS;
  260. if (!cs->connected) {
  261. gig_dbg(DEBUG_IF, "not connected");
  262. retval = -ENODEV;
  263. goto done;
  264. }
  265. if (cs->mstate != MS_LOCKED) {
  266. dev_warn(cs->dev, "can't write to unlocked device\n");
  267. retval = -EBUSY;
  268. goto done;
  269. }
  270. if (count <= 0) {
  271. /* nothing to do */
  272. retval = 0;
  273. goto done;
  274. }
  275. cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
  276. if (!cb) {
  277. dev_err(cs->dev, "%s: out of memory\n", __func__);
  278. retval = -ENOMEM;
  279. goto done;
  280. }
  281. memcpy(cb->buf, buf, count);
  282. cb->len = count;
  283. cb->offset = 0;
  284. cb->next = NULL;
  285. cb->wake_tasklet = &cs->if_wake_tasklet;
  286. retval = cs->ops->write_cmd(cs, cb);
  287. done:
  288. mutex_unlock(&cs->mutex);
  289. return retval;
  290. }
  291. static int if_write_room(struct tty_struct *tty)
  292. {
  293. struct cardstate *cs = tty->driver_data;
  294. int retval = -ENODEV;
  295. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  296. if (mutex_lock_interruptible(&cs->mutex))
  297. return -ERESTARTSYS;
  298. if (!cs->connected) {
  299. gig_dbg(DEBUG_IF, "not connected");
  300. retval = -ENODEV;
  301. } else if (cs->mstate != MS_LOCKED) {
  302. dev_warn(cs->dev, "can't write to unlocked device\n");
  303. retval = -EBUSY;
  304. } else
  305. retval = cs->ops->write_room(cs);
  306. mutex_unlock(&cs->mutex);
  307. return retval;
  308. }
  309. static int if_chars_in_buffer(struct tty_struct *tty)
  310. {
  311. struct cardstate *cs = tty->driver_data;
  312. int retval = 0;
  313. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  314. mutex_lock(&cs->mutex);
  315. if (!cs->connected)
  316. gig_dbg(DEBUG_IF, "not connected");
  317. else if (cs->mstate != MS_LOCKED)
  318. dev_warn(cs->dev, "can't write to unlocked device\n");
  319. else
  320. retval = cs->ops->chars_in_buffer(cs);
  321. mutex_unlock(&cs->mutex);
  322. return retval;
  323. }
  324. static void if_throttle(struct tty_struct *tty)
  325. {
  326. struct cardstate *cs = tty->driver_data;
  327. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  328. mutex_lock(&cs->mutex);
  329. if (!cs->connected)
  330. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  331. else
  332. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  333. mutex_unlock(&cs->mutex);
  334. }
  335. static void if_unthrottle(struct tty_struct *tty)
  336. {
  337. struct cardstate *cs = tty->driver_data;
  338. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  339. mutex_lock(&cs->mutex);
  340. if (!cs->connected)
  341. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  342. else
  343. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  344. mutex_unlock(&cs->mutex);
  345. }
  346. static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
  347. {
  348. struct cardstate *cs = tty->driver_data;
  349. unsigned int iflag;
  350. unsigned int cflag;
  351. unsigned int old_cflag;
  352. unsigned int control_state, new_state;
  353. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  354. mutex_lock(&cs->mutex);
  355. if (!cs->connected) {
  356. gig_dbg(DEBUG_IF, "not connected");
  357. goto out;
  358. }
  359. iflag = tty->termios->c_iflag;
  360. cflag = tty->termios->c_cflag;
  361. old_cflag = old ? old->c_cflag : cflag;
  362. gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
  363. cs->minor_index, iflag, cflag, old_cflag);
  364. /* get a local copy of the current port settings */
  365. control_state = cs->control_state;
  366. /*
  367. * Update baud rate.
  368. * Do not attempt to cache old rates and skip settings,
  369. * disconnects screw such tricks up completely.
  370. * Premature optimization is the root of all evil.
  371. */
  372. /* reassert DTR and (maybe) RTS on transition from B0 */
  373. if ((old_cflag & CBAUD) == B0) {
  374. new_state = control_state | TIOCM_DTR;
  375. /* don't set RTS if using hardware flow control */
  376. if (!(old_cflag & CRTSCTS))
  377. new_state |= TIOCM_RTS;
  378. gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
  379. cs->minor_index,
  380. (new_state & TIOCM_RTS) ? " only" : "/RTS");
  381. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  382. control_state = new_state;
  383. }
  384. cs->ops->baud_rate(cs, cflag & CBAUD);
  385. if ((cflag & CBAUD) == B0) {
  386. /* Drop RTS and DTR */
  387. gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
  388. new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
  389. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  390. control_state = new_state;
  391. }
  392. /*
  393. * Update line control register (LCR)
  394. */
  395. cs->ops->set_line_ctrl(cs, cflag);
  396. /* save off the modified port settings */
  397. cs->control_state = control_state;
  398. out:
  399. mutex_unlock(&cs->mutex);
  400. }
  401. /* wakeup tasklet for the write operation */
  402. static void if_wake(unsigned long data)
  403. {
  404. struct cardstate *cs = (struct cardstate *)data;
  405. struct tty_struct *tty = tty_port_tty_get(&cs->port);
  406. if (tty) {
  407. tty_wakeup(tty);
  408. tty_kref_put(tty);
  409. }
  410. }
  411. /*** interface to common ***/
  412. void gigaset_if_init(struct cardstate *cs)
  413. {
  414. struct gigaset_driver *drv;
  415. drv = cs->driver;
  416. if (!drv->have_tty)
  417. return;
  418. tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
  419. mutex_lock(&cs->mutex);
  420. cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
  421. if (!IS_ERR(cs->tty_dev))
  422. dev_set_drvdata(cs->tty_dev, cs);
  423. else {
  424. pr_warning("could not register device to the tty subsystem\n");
  425. cs->tty_dev = NULL;
  426. }
  427. mutex_unlock(&cs->mutex);
  428. }
  429. void gigaset_if_free(struct cardstate *cs)
  430. {
  431. struct gigaset_driver *drv;
  432. drv = cs->driver;
  433. if (!drv->have_tty)
  434. return;
  435. tasklet_disable(&cs->if_wake_tasklet);
  436. tasklet_kill(&cs->if_wake_tasklet);
  437. cs->tty_dev = NULL;
  438. tty_unregister_device(drv->tty, cs->minor_index);
  439. }
  440. /**
  441. * gigaset_if_receive() - pass a received block of data to the tty device
  442. * @cs: device descriptor structure.
  443. * @buffer: received data.
  444. * @len: number of bytes received.
  445. *
  446. * Called by asyncdata/isocdata if a block of data received from the
  447. * device must be sent to userspace through the ttyG* device.
  448. */
  449. void gigaset_if_receive(struct cardstate *cs,
  450. unsigned char *buffer, size_t len)
  451. {
  452. struct tty_struct *tty = tty_port_tty_get(&cs->port);
  453. if (tty == NULL) {
  454. gig_dbg(DEBUG_IF, "receive on closed device");
  455. return;
  456. }
  457. tty_insert_flip_string(tty, buffer, len);
  458. tty_flip_buffer_push(tty);
  459. tty_kref_put(tty);
  460. }
  461. EXPORT_SYMBOL_GPL(gigaset_if_receive);
  462. /* gigaset_if_initdriver
  463. * Initialize tty interface.
  464. * parameters:
  465. * drv Driver
  466. * procname Name of the driver (e.g. for /proc/tty/drivers)
  467. * devname Name of the device files (prefix without minor number)
  468. */
  469. void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
  470. const char *devname)
  471. {
  472. int ret;
  473. struct tty_driver *tty;
  474. drv->have_tty = 0;
  475. drv->tty = tty = alloc_tty_driver(drv->minors);
  476. if (tty == NULL)
  477. goto enomem;
  478. tty->type = TTY_DRIVER_TYPE_SERIAL;
  479. tty->subtype = SERIAL_TYPE_NORMAL;
  480. tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  481. tty->driver_name = procname;
  482. tty->name = devname;
  483. tty->minor_start = drv->minor;
  484. tty->init_termios = tty_std_termios;
  485. tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  486. tty_set_operations(tty, &if_ops);
  487. ret = tty_register_driver(tty);
  488. if (ret < 0) {
  489. pr_err("error %d registering tty driver\n", ret);
  490. goto error;
  491. }
  492. gig_dbg(DEBUG_IF, "tty driver initialized");
  493. drv->have_tty = 1;
  494. return;
  495. enomem:
  496. pr_err("out of memory\n");
  497. error:
  498. if (drv->tty)
  499. put_tty_driver(drv->tty);
  500. }
  501. void gigaset_if_freedriver(struct gigaset_driver *drv)
  502. {
  503. if (!drv->have_tty)
  504. return;
  505. drv->have_tty = 0;
  506. tty_unregister_driver(drv->tty);
  507. put_tty_driver(drv->tty);
  508. }