interface.c 16 KB

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