interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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, (const unsigned char *) arg);
  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. static int if_tiocmget(struct tty_struct *tty)
  193. {
  194. struct cardstate *cs = tty->driver_data;
  195. int retval;
  196. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  197. if (mutex_lock_interruptible(&cs->mutex))
  198. return -ERESTARTSYS;
  199. retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
  200. mutex_unlock(&cs->mutex);
  201. return retval;
  202. }
  203. static int if_tiocmset(struct tty_struct *tty,
  204. unsigned int set, unsigned int clear)
  205. {
  206. struct cardstate *cs = tty->driver_data;
  207. int retval;
  208. unsigned mc;
  209. gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
  210. cs->minor_index, __func__, set, clear);
  211. if (mutex_lock_interruptible(&cs->mutex))
  212. return -ERESTARTSYS;
  213. if (!cs->connected) {
  214. gig_dbg(DEBUG_IF, "not connected");
  215. retval = -ENODEV;
  216. } else {
  217. mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
  218. retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
  219. cs->control_state = mc;
  220. }
  221. mutex_unlock(&cs->mutex);
  222. return retval;
  223. }
  224. static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
  225. {
  226. struct cardstate *cs = tty->driver_data;
  227. struct cmdbuf_t *cb;
  228. int retval;
  229. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  230. if (mutex_lock_interruptible(&cs->mutex))
  231. return -ERESTARTSYS;
  232. if (!cs->connected) {
  233. gig_dbg(DEBUG_IF, "not connected");
  234. retval = -ENODEV;
  235. goto done;
  236. }
  237. if (cs->mstate != MS_LOCKED) {
  238. dev_warn(cs->dev, "can't write to unlocked device\n");
  239. retval = -EBUSY;
  240. goto done;
  241. }
  242. if (count <= 0) {
  243. /* nothing to do */
  244. retval = 0;
  245. goto done;
  246. }
  247. cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
  248. if (!cb) {
  249. dev_err(cs->dev, "%s: out of memory\n", __func__);
  250. retval = -ENOMEM;
  251. goto done;
  252. }
  253. memcpy(cb->buf, buf, count);
  254. cb->len = count;
  255. cb->offset = 0;
  256. cb->next = NULL;
  257. cb->wake_tasklet = &cs->if_wake_tasklet;
  258. retval = cs->ops->write_cmd(cs, cb);
  259. done:
  260. mutex_unlock(&cs->mutex);
  261. return retval;
  262. }
  263. static int if_write_room(struct tty_struct *tty)
  264. {
  265. struct cardstate *cs = tty->driver_data;
  266. int retval;
  267. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  268. if (mutex_lock_interruptible(&cs->mutex))
  269. return -ERESTARTSYS;
  270. if (!cs->connected) {
  271. gig_dbg(DEBUG_IF, "not connected");
  272. retval = -ENODEV;
  273. } else if (cs->mstate != MS_LOCKED) {
  274. dev_warn(cs->dev, "can't write to unlocked device\n");
  275. retval = -EBUSY;
  276. } else
  277. retval = cs->ops->write_room(cs);
  278. mutex_unlock(&cs->mutex);
  279. return retval;
  280. }
  281. static int if_chars_in_buffer(struct tty_struct *tty)
  282. {
  283. struct cardstate *cs = tty->driver_data;
  284. int retval = 0;
  285. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  286. mutex_lock(&cs->mutex);
  287. if (!cs->connected)
  288. gig_dbg(DEBUG_IF, "not connected");
  289. else if (cs->mstate != MS_LOCKED)
  290. dev_warn(cs->dev, "can't write to unlocked device\n");
  291. else
  292. retval = cs->ops->chars_in_buffer(cs);
  293. mutex_unlock(&cs->mutex);
  294. return retval;
  295. }
  296. static void if_throttle(struct tty_struct *tty)
  297. {
  298. struct cardstate *cs = tty->driver_data;
  299. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  300. mutex_lock(&cs->mutex);
  301. if (!cs->connected)
  302. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  303. else
  304. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  305. mutex_unlock(&cs->mutex);
  306. }
  307. static void if_unthrottle(struct tty_struct *tty)
  308. {
  309. struct cardstate *cs = tty->driver_data;
  310. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  311. mutex_lock(&cs->mutex);
  312. if (!cs->connected)
  313. gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
  314. else
  315. gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
  316. mutex_unlock(&cs->mutex);
  317. }
  318. static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
  319. {
  320. struct cardstate *cs = tty->driver_data;
  321. unsigned int iflag;
  322. unsigned int cflag;
  323. unsigned int old_cflag;
  324. unsigned int control_state, new_state;
  325. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  326. mutex_lock(&cs->mutex);
  327. if (!cs->connected) {
  328. gig_dbg(DEBUG_IF, "not connected");
  329. goto out;
  330. }
  331. iflag = tty->termios.c_iflag;
  332. cflag = tty->termios.c_cflag;
  333. old_cflag = old ? old->c_cflag : cflag;
  334. gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
  335. cs->minor_index, iflag, cflag, old_cflag);
  336. /* get a local copy of the current port settings */
  337. control_state = cs->control_state;
  338. /*
  339. * Update baud rate.
  340. * Do not attempt to cache old rates and skip settings,
  341. * disconnects screw such tricks up completely.
  342. * Premature optimization is the root of all evil.
  343. */
  344. /* reassert DTR and (maybe) RTS on transition from B0 */
  345. if ((old_cflag & CBAUD) == B0) {
  346. new_state = control_state | TIOCM_DTR;
  347. /* don't set RTS if using hardware flow control */
  348. if (!(old_cflag & CRTSCTS))
  349. new_state |= TIOCM_RTS;
  350. gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
  351. cs->minor_index,
  352. (new_state & TIOCM_RTS) ? " only" : "/RTS");
  353. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  354. control_state = new_state;
  355. }
  356. cs->ops->baud_rate(cs, cflag & CBAUD);
  357. if ((cflag & CBAUD) == B0) {
  358. /* Drop RTS and DTR */
  359. gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
  360. new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
  361. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  362. control_state = new_state;
  363. }
  364. /*
  365. * Update line control register (LCR)
  366. */
  367. cs->ops->set_line_ctrl(cs, cflag);
  368. /* save off the modified port settings */
  369. cs->control_state = control_state;
  370. out:
  371. mutex_unlock(&cs->mutex);
  372. }
  373. static const struct tty_operations if_ops = {
  374. .open = if_open,
  375. .close = if_close,
  376. .ioctl = if_ioctl,
  377. .write = if_write,
  378. .write_room = if_write_room,
  379. .chars_in_buffer = if_chars_in_buffer,
  380. .set_termios = if_set_termios,
  381. .throttle = if_throttle,
  382. .unthrottle = if_unthrottle,
  383. .tiocmget = if_tiocmget,
  384. .tiocmset = if_tiocmset,
  385. };
  386. /* wakeup tasklet for the write operation */
  387. static void if_wake(unsigned long data)
  388. {
  389. struct cardstate *cs = (struct cardstate *)data;
  390. tty_port_tty_wakeup(&cs->port);
  391. }
  392. /*** interface to common ***/
  393. void gigaset_if_init(struct cardstate *cs)
  394. {
  395. struct gigaset_driver *drv;
  396. drv = cs->driver;
  397. if (!drv->have_tty)
  398. return;
  399. tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
  400. mutex_lock(&cs->mutex);
  401. cs->tty_dev = tty_port_register_device(&cs->port, drv->tty,
  402. cs->minor_index, NULL);
  403. if (!IS_ERR(cs->tty_dev))
  404. dev_set_drvdata(cs->tty_dev, cs);
  405. else {
  406. pr_warning("could not register device to the tty subsystem\n");
  407. cs->tty_dev = NULL;
  408. }
  409. mutex_unlock(&cs->mutex);
  410. }
  411. void gigaset_if_free(struct cardstate *cs)
  412. {
  413. struct gigaset_driver *drv;
  414. drv = cs->driver;
  415. if (!drv->have_tty)
  416. return;
  417. tasklet_disable(&cs->if_wake_tasklet);
  418. tasklet_kill(&cs->if_wake_tasklet);
  419. cs->tty_dev = NULL;
  420. tty_unregister_device(drv->tty, cs->minor_index);
  421. }
  422. /**
  423. * gigaset_if_receive() - pass a received block of data to the tty device
  424. * @cs: device descriptor structure.
  425. * @buffer: received data.
  426. * @len: number of bytes received.
  427. *
  428. * Called by asyncdata/isocdata if a block of data received from the
  429. * device must be sent to userspace through the ttyG* device.
  430. */
  431. void gigaset_if_receive(struct cardstate *cs,
  432. unsigned char *buffer, size_t len)
  433. {
  434. tty_insert_flip_string(&cs->port, buffer, len);
  435. tty_flip_buffer_push(&cs->port);
  436. }
  437. EXPORT_SYMBOL_GPL(gigaset_if_receive);
  438. /* gigaset_if_initdriver
  439. * Initialize tty interface.
  440. * parameters:
  441. * drv Driver
  442. * procname Name of the driver (e.g. for /proc/tty/drivers)
  443. * devname Name of the device files (prefix without minor number)
  444. */
  445. void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
  446. const char *devname)
  447. {
  448. int ret;
  449. struct tty_driver *tty;
  450. drv->have_tty = 0;
  451. drv->tty = tty = alloc_tty_driver(drv->minors);
  452. if (tty == NULL)
  453. goto enomem;
  454. tty->type = TTY_DRIVER_TYPE_SERIAL;
  455. tty->subtype = SERIAL_TYPE_NORMAL;
  456. tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  457. tty->driver_name = procname;
  458. tty->name = devname;
  459. tty->minor_start = drv->minor;
  460. tty->init_termios = tty_std_termios;
  461. tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  462. tty_set_operations(tty, &if_ops);
  463. ret = tty_register_driver(tty);
  464. if (ret < 0) {
  465. pr_err("error %d registering tty driver\n", ret);
  466. goto error;
  467. }
  468. gig_dbg(DEBUG_IF, "tty driver initialized");
  469. drv->have_tty = 1;
  470. return;
  471. enomem:
  472. pr_err("out of memory\n");
  473. error:
  474. if (drv->tty)
  475. put_tty_driver(drv->tty);
  476. }
  477. void gigaset_if_freedriver(struct gigaset_driver *drv)
  478. {
  479. if (!drv->have_tty)
  480. return;
  481. drv->have_tty = 0;
  482. tty_unregister_driver(drv->tty);
  483. put_tty_driver(drv->tty);
  484. }