interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. static int if_open(struct tty_struct *tty, struct file *filp)
  93. {
  94. struct cardstate *cs;
  95. gig_dbg(DEBUG_IF, "%d+%d: %s()",
  96. tty->driver->minor_start, tty->index, __func__);
  97. cs = gigaset_get_cs_by_tty(tty);
  98. if (!cs || !try_module_get(cs->driver->owner))
  99. return -ENODEV;
  100. if (mutex_lock_interruptible(&cs->mutex)) {
  101. module_put(cs->driver->owner);
  102. return -ERESTARTSYS;
  103. }
  104. tty->driver_data = cs;
  105. ++cs->port.count;
  106. if (cs->port.count == 1) {
  107. tty_port_tty_set(&cs->port, tty);
  108. cs->port.low_latency = 1;
  109. }
  110. mutex_unlock(&cs->mutex);
  111. return 0;
  112. }
  113. static void if_close(struct tty_struct *tty, struct file *filp)
  114. {
  115. struct cardstate *cs = tty->driver_data;
  116. if (!cs) { /* happens if we didn't find cs in open */
  117. gig_dbg(DEBUG_IF, "%s: no cardstate", __func__);
  118. return;
  119. }
  120. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  121. mutex_lock(&cs->mutex);
  122. if (!cs->connected)
  123. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  124. else if (!cs->port.count)
  125. dev_warn(cs->dev, "%s: device not opened\n", __func__);
  126. else if (!--cs->port.count)
  127. tty_port_tty_set(&cs->port, NULL);
  128. mutex_unlock(&cs->mutex);
  129. module_put(cs->driver->owner);
  130. }
  131. static int if_ioctl(struct tty_struct *tty,
  132. unsigned int cmd, unsigned long arg)
  133. {
  134. struct cardstate *cs = tty->driver_data;
  135. int retval = -ENODEV;
  136. int int_arg;
  137. unsigned char buf[6];
  138. unsigned version[4];
  139. gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
  140. if (mutex_lock_interruptible(&cs->mutex))
  141. return -ERESTARTSYS;
  142. if (!cs->connected) {
  143. gig_dbg(DEBUG_IF, "not connected");
  144. retval = -ENODEV;
  145. } else {
  146. retval = 0;
  147. switch (cmd) {
  148. case GIGASET_REDIR:
  149. retval = get_user(int_arg, (int __user *) arg);
  150. if (retval >= 0)
  151. retval = if_lock(cs, &int_arg);
  152. if (retval >= 0)
  153. retval = put_user(int_arg, (int __user *) arg);
  154. break;
  155. case GIGASET_CONFIG:
  156. retval = get_user(int_arg, (int __user *) arg);
  157. if (retval >= 0)
  158. retval = if_config(cs, &int_arg);
  159. if (retval >= 0)
  160. retval = put_user(int_arg, (int __user *) arg);
  161. break;
  162. case GIGASET_BRKCHARS:
  163. retval = copy_from_user(&buf,
  164. (const unsigned char __user *) arg, 6)
  165. ? -EFAULT : 0;
  166. if (retval >= 0) {
  167. gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
  168. 6, buf);
  169. retval = cs->ops->brkchars(cs, buf);
  170. }
  171. break;
  172. case GIGASET_VERSION:
  173. retval = copy_from_user(version,
  174. (unsigned __user *) arg, sizeof version)
  175. ? -EFAULT : 0;
  176. if (retval >= 0)
  177. retval = if_version(cs, version);
  178. if (retval >= 0)
  179. retval = copy_to_user((unsigned __user *) arg,
  180. version, sizeof version)
  181. ? -EFAULT : 0;
  182. break;
  183. default:
  184. gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
  185. __func__, cmd);
  186. retval = -ENOIOCTLCMD;
  187. }
  188. }
  189. mutex_unlock(&cs->mutex);
  190. return retval;
  191. }
  192. #ifdef CONFIG_COMPAT
  193. static long if_compat_ioctl(struct tty_struct *tty,
  194. unsigned int cmd, unsigned long arg)
  195. {
  196. return if_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
  197. }
  198. #endif
  199. static int if_tiocmget(struct tty_struct *tty)
  200. {
  201. struct cardstate *cs = tty->driver_data;
  202. int retval;
  203. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  204. if (mutex_lock_interruptible(&cs->mutex))
  205. return -ERESTARTSYS;
  206. retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
  207. mutex_unlock(&cs->mutex);
  208. return retval;
  209. }
  210. static int if_tiocmset(struct tty_struct *tty,
  211. unsigned int set, unsigned int clear)
  212. {
  213. struct cardstate *cs = tty->driver_data;
  214. int retval;
  215. unsigned mc;
  216. gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
  217. cs->minor_index, __func__, set, clear);
  218. if (mutex_lock_interruptible(&cs->mutex))
  219. return -ERESTARTSYS;
  220. if (!cs->connected) {
  221. gig_dbg(DEBUG_IF, "not connected");
  222. retval = -ENODEV;
  223. } else {
  224. mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
  225. retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
  226. cs->control_state = mc;
  227. }
  228. mutex_unlock(&cs->mutex);
  229. return retval;
  230. }
  231. static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
  232. {
  233. struct cardstate *cs = tty->driver_data;
  234. struct cmdbuf_t *cb;
  235. int retval;
  236. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  237. if (mutex_lock_interruptible(&cs->mutex))
  238. return -ERESTARTSYS;
  239. if (!cs->connected) {
  240. gig_dbg(DEBUG_IF, "not connected");
  241. retval = -ENODEV;
  242. goto done;
  243. }
  244. if (cs->mstate != MS_LOCKED) {
  245. dev_warn(cs->dev, "can't write to unlocked device\n");
  246. retval = -EBUSY;
  247. goto done;
  248. }
  249. if (count <= 0) {
  250. /* nothing to do */
  251. retval = 0;
  252. goto done;
  253. }
  254. cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
  255. if (!cb) {
  256. dev_err(cs->dev, "%s: out of memory\n", __func__);
  257. retval = -ENOMEM;
  258. goto done;
  259. }
  260. memcpy(cb->buf, buf, count);
  261. cb->len = count;
  262. cb->offset = 0;
  263. cb->next = NULL;
  264. cb->wake_tasklet = &cs->if_wake_tasklet;
  265. retval = cs->ops->write_cmd(cs, cb);
  266. done:
  267. mutex_unlock(&cs->mutex);
  268. return retval;
  269. }
  270. static int if_write_room(struct tty_struct *tty)
  271. {
  272. struct cardstate *cs = tty->driver_data;
  273. int retval;
  274. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  275. if (mutex_lock_interruptible(&cs->mutex))
  276. return -ERESTARTSYS;
  277. if (!cs->connected) {
  278. gig_dbg(DEBUG_IF, "not connected");
  279. retval = -ENODEV;
  280. } else if (cs->mstate != MS_LOCKED) {
  281. dev_warn(cs->dev, "can't write to unlocked device\n");
  282. retval = -EBUSY;
  283. } else
  284. retval = cs->ops->write_room(cs);
  285. mutex_unlock(&cs->mutex);
  286. return retval;
  287. }
  288. static int if_chars_in_buffer(struct tty_struct *tty)
  289. {
  290. struct cardstate *cs = tty->driver_data;
  291. int retval = 0;
  292. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  293. mutex_lock(&cs->mutex);
  294. if (!cs->connected)
  295. gig_dbg(DEBUG_IF, "not connected");
  296. else if (cs->mstate != MS_LOCKED)
  297. dev_warn(cs->dev, "can't write to unlocked device\n");
  298. else
  299. retval = cs->ops->chars_in_buffer(cs);
  300. mutex_unlock(&cs->mutex);
  301. return retval;
  302. }
  303. static void if_throttle(struct tty_struct *tty)
  304. {
  305. struct cardstate *cs = tty->driver_data;
  306. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  307. mutex_lock(&cs->mutex);
  308. if (!cs->connected)
  309. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  310. else
  311. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  312. mutex_unlock(&cs->mutex);
  313. }
  314. static void if_unthrottle(struct tty_struct *tty)
  315. {
  316. struct cardstate *cs = tty->driver_data;
  317. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  318. mutex_lock(&cs->mutex);
  319. if (!cs->connected)
  320. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  321. else
  322. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  323. mutex_unlock(&cs->mutex);
  324. }
  325. static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
  326. {
  327. struct cardstate *cs = tty->driver_data;
  328. unsigned int iflag;
  329. unsigned int cflag;
  330. unsigned int old_cflag;
  331. unsigned int control_state, new_state;
  332. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  333. mutex_lock(&cs->mutex);
  334. if (!cs->connected) {
  335. gig_dbg(DEBUG_IF, "not connected");
  336. goto out;
  337. }
  338. iflag = tty->termios.c_iflag;
  339. cflag = tty->termios.c_cflag;
  340. old_cflag = old ? old->c_cflag : cflag;
  341. gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
  342. cs->minor_index, iflag, cflag, old_cflag);
  343. /* get a local copy of the current port settings */
  344. control_state = cs->control_state;
  345. /*
  346. * Update baud rate.
  347. * Do not attempt to cache old rates and skip settings,
  348. * disconnects screw such tricks up completely.
  349. * Premature optimization is the root of all evil.
  350. */
  351. /* reassert DTR and (maybe) RTS on transition from B0 */
  352. if ((old_cflag & CBAUD) == B0) {
  353. new_state = control_state | TIOCM_DTR;
  354. /* don't set RTS if using hardware flow control */
  355. if (!(old_cflag & CRTSCTS))
  356. new_state |= TIOCM_RTS;
  357. gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
  358. cs->minor_index,
  359. (new_state & TIOCM_RTS) ? " only" : "/RTS");
  360. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  361. control_state = new_state;
  362. }
  363. cs->ops->baud_rate(cs, cflag & CBAUD);
  364. if ((cflag & CBAUD) == B0) {
  365. /* Drop RTS and DTR */
  366. gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
  367. new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
  368. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  369. control_state = new_state;
  370. }
  371. /*
  372. * Update line control register (LCR)
  373. */
  374. cs->ops->set_line_ctrl(cs, cflag);
  375. /* save off the modified port settings */
  376. cs->control_state = control_state;
  377. out:
  378. mutex_unlock(&cs->mutex);
  379. }
  380. static const struct tty_operations if_ops = {
  381. .open = if_open,
  382. .close = if_close,
  383. .ioctl = if_ioctl,
  384. #ifdef CONFIG_COMPAT
  385. .compat_ioctl = if_compat_ioctl,
  386. #endif
  387. .write = if_write,
  388. .write_room = if_write_room,
  389. .chars_in_buffer = if_chars_in_buffer,
  390. .set_termios = if_set_termios,
  391. .throttle = if_throttle,
  392. .unthrottle = if_unthrottle,
  393. .tiocmget = if_tiocmget,
  394. .tiocmset = if_tiocmset,
  395. };
  396. /* wakeup tasklet for the write operation */
  397. static void if_wake(unsigned long data)
  398. {
  399. struct cardstate *cs = (struct cardstate *)data;
  400. tty_port_tty_wakeup(&cs->port);
  401. }
  402. /*** interface to common ***/
  403. void gigaset_if_init(struct cardstate *cs)
  404. {
  405. struct gigaset_driver *drv;
  406. drv = cs->driver;
  407. if (!drv->have_tty)
  408. return;
  409. tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
  410. mutex_lock(&cs->mutex);
  411. cs->tty_dev = tty_port_register_device(&cs->port, drv->tty,
  412. cs->minor_index, NULL);
  413. if (!IS_ERR(cs->tty_dev))
  414. dev_set_drvdata(cs->tty_dev, cs);
  415. else {
  416. pr_warning("could not register device to the tty subsystem\n");
  417. cs->tty_dev = NULL;
  418. }
  419. mutex_unlock(&cs->mutex);
  420. }
  421. void gigaset_if_free(struct cardstate *cs)
  422. {
  423. struct gigaset_driver *drv;
  424. drv = cs->driver;
  425. if (!drv->have_tty)
  426. return;
  427. tasklet_disable(&cs->if_wake_tasklet);
  428. tasklet_kill(&cs->if_wake_tasklet);
  429. cs->tty_dev = NULL;
  430. tty_unregister_device(drv->tty, cs->minor_index);
  431. }
  432. /**
  433. * gigaset_if_receive() - pass a received block of data to the tty device
  434. * @cs: device descriptor structure.
  435. * @buffer: received data.
  436. * @len: number of bytes received.
  437. *
  438. * Called by asyncdata/isocdata if a block of data received from the
  439. * device must be sent to userspace through the ttyG* device.
  440. */
  441. void gigaset_if_receive(struct cardstate *cs,
  442. unsigned char *buffer, size_t len)
  443. {
  444. tty_insert_flip_string(&cs->port, buffer, len);
  445. tty_flip_buffer_push(&cs->port);
  446. }
  447. EXPORT_SYMBOL_GPL(gigaset_if_receive);
  448. /* gigaset_if_initdriver
  449. * Initialize tty interface.
  450. * parameters:
  451. * drv Driver
  452. * procname Name of the driver (e.g. for /proc/tty/drivers)
  453. * devname Name of the device files (prefix without minor number)
  454. */
  455. void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
  456. const char *devname)
  457. {
  458. int ret;
  459. struct tty_driver *tty;
  460. drv->have_tty = 0;
  461. drv->tty = tty = alloc_tty_driver(drv->minors);
  462. if (tty == NULL)
  463. goto enomem;
  464. tty->type = TTY_DRIVER_TYPE_SERIAL;
  465. tty->subtype = SERIAL_TYPE_NORMAL;
  466. tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  467. tty->driver_name = procname;
  468. tty->name = devname;
  469. tty->minor_start = drv->minor;
  470. tty->init_termios = tty_std_termios;
  471. tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  472. tty_set_operations(tty, &if_ops);
  473. ret = tty_register_driver(tty);
  474. if (ret < 0) {
  475. pr_err("error %d registering tty driver\n", ret);
  476. goto error;
  477. }
  478. gig_dbg(DEBUG_IF, "tty driver initialized");
  479. drv->have_tty = 1;
  480. return;
  481. enomem:
  482. pr_err("out of memory\n");
  483. error:
  484. if (drv->tty)
  485. put_tty_driver(drv->tty);
  486. }
  487. void gigaset_if_freedriver(struct gigaset_driver *drv)
  488. {
  489. if (!drv->have_tty)
  490. return;
  491. drv->have_tty = 0;
  492. tty_unregister_driver(drv->tty);
  493. put_tty_driver(drv->tty);
  494. }