interface.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 = atomic_read(&cs->mstate) == MS_LOCKED; //FIXME remove?
  26. return 0;
  27. }
  28. if (!cmd && atomic_read(&cs->mstate) == MS_LOCKED
  29. && cs->connected) {
  30. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  31. cs->ops->baud_rate(cs, B115200);
  32. cs->ops->set_line_ctrl(cs, CS8);
  33. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  34. }
  35. cs->waiting = 1;
  36. if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
  37. NULL, cmd, NULL)) {
  38. cs->waiting = 0;
  39. return -ENOMEM;
  40. }
  41. gig_dbg(DEBUG_CMD, "scheduling IF_LOCK");
  42. gigaset_schedule_event(cs);
  43. wait_event(cs->waitqueue, !cs->waiting);
  44. if (cs->cmd_result >= 0) {
  45. *arg = cs->cmd_result;
  46. return 0;
  47. }
  48. return cs->cmd_result;
  49. }
  50. static int if_version(struct cardstate *cs, unsigned arg[4])
  51. {
  52. static const unsigned version[4] = GIG_VERSION;
  53. static const unsigned compat[4] = GIG_COMPAT;
  54. unsigned cmd = arg[0];
  55. gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
  56. switch (cmd) {
  57. case GIGVER_DRIVER:
  58. memcpy(arg, version, sizeof version);
  59. return 0;
  60. case GIGVER_COMPAT:
  61. memcpy(arg, compat, sizeof compat);
  62. return 0;
  63. case GIGVER_FWBASE:
  64. cs->waiting = 1;
  65. if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
  66. NULL, 0, arg)) {
  67. cs->waiting = 0;
  68. return -ENOMEM;
  69. }
  70. gig_dbg(DEBUG_CMD, "scheduling IF_VER");
  71. gigaset_schedule_event(cs);
  72. wait_event(cs->waitqueue, !cs->waiting);
  73. if (cs->cmd_result >= 0)
  74. return 0;
  75. return cs->cmd_result;
  76. default:
  77. return -EINVAL;
  78. }
  79. }
  80. static int if_config(struct cardstate *cs, int *arg)
  81. {
  82. gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
  83. if (*arg != 1)
  84. return -EINVAL;
  85. if (atomic_read(&cs->mstate) != MS_LOCKED)
  86. return -EBUSY;
  87. if (!cs->connected) {
  88. err("not connected!");
  89. return -ENODEV;
  90. }
  91. *arg = 0;
  92. return gigaset_enterconfigmode(cs);
  93. }
  94. /*** the terminal driver ***/
  95. /* stolen from usbserial and some other tty drivers */
  96. static int if_open(struct tty_struct *tty, struct file *filp);
  97. static void if_close(struct tty_struct *tty, struct file *filp);
  98. static int if_ioctl(struct tty_struct *tty, struct file *file,
  99. unsigned int cmd, unsigned long arg);
  100. static int if_write_room(struct tty_struct *tty);
  101. static int if_chars_in_buffer(struct tty_struct *tty);
  102. static void if_throttle(struct tty_struct *tty);
  103. static void if_unthrottle(struct tty_struct *tty);
  104. static void if_set_termios(struct tty_struct *tty, struct termios *old);
  105. static int if_tiocmget(struct tty_struct *tty, struct file *file);
  106. static int if_tiocmset(struct tty_struct *tty, struct file *file,
  107. unsigned int set, unsigned int clear);
  108. static int if_write(struct tty_struct *tty,
  109. const unsigned char *buf, int count);
  110. static const struct tty_operations if_ops = {
  111. .open = if_open,
  112. .close = if_close,
  113. .ioctl = if_ioctl,
  114. .write = if_write,
  115. .write_room = if_write_room,
  116. .chars_in_buffer = if_chars_in_buffer,
  117. .set_termios = if_set_termios,
  118. .throttle = if_throttle,
  119. .unthrottle = if_unthrottle,
  120. #if 0
  121. .break_ctl = serial_break,
  122. #endif
  123. .tiocmget = if_tiocmget,
  124. .tiocmset = if_tiocmset,
  125. };
  126. static int if_open(struct tty_struct *tty, struct file *filp)
  127. {
  128. struct cardstate *cs;
  129. unsigned long flags;
  130. gig_dbg(DEBUG_IF, "%d+%d: %s()",
  131. tty->driver->minor_start, tty->index, __func__);
  132. tty->driver_data = NULL;
  133. cs = gigaset_get_cs_by_tty(tty);
  134. if (!cs)
  135. return -ENODEV;
  136. if (mutex_lock_interruptible(&cs->mutex))
  137. return -ERESTARTSYS; // FIXME -EINTR?
  138. tty->driver_data = cs;
  139. ++cs->open_count;
  140. if (cs->open_count == 1) {
  141. spin_lock_irqsave(&cs->lock, flags);
  142. cs->tty = tty;
  143. spin_unlock_irqrestore(&cs->lock, flags);
  144. tty->low_latency = 1; //FIXME test
  145. }
  146. mutex_unlock(&cs->mutex);
  147. return 0;
  148. }
  149. static void if_close(struct tty_struct *tty, struct file *filp)
  150. {
  151. struct cardstate *cs;
  152. unsigned long flags;
  153. cs = (struct cardstate *) tty->driver_data;
  154. if (!cs) {
  155. err("cs==NULL in %s", __func__);
  156. return;
  157. }
  158. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  159. mutex_lock(&cs->mutex);
  160. if (!cs->open_count)
  161. warn("%s: device not opened", __func__);
  162. else {
  163. if (!--cs->open_count) {
  164. spin_lock_irqsave(&cs->lock, flags);
  165. cs->tty = NULL;
  166. spin_unlock_irqrestore(&cs->lock, flags);
  167. }
  168. }
  169. mutex_unlock(&cs->mutex);
  170. }
  171. static int if_ioctl(struct tty_struct *tty, struct file *file,
  172. unsigned int cmd, unsigned long arg)
  173. {
  174. struct cardstate *cs;
  175. int retval = -ENODEV;
  176. int int_arg;
  177. unsigned char buf[6];
  178. unsigned version[4];
  179. cs = (struct cardstate *) tty->driver_data;
  180. if (!cs) {
  181. err("cs==NULL in %s", __func__);
  182. return -ENODEV;
  183. }
  184. gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
  185. if (mutex_lock_interruptible(&cs->mutex))
  186. return -ERESTARTSYS; // FIXME -EINTR?
  187. if (!cs->open_count)
  188. warn("%s: device not opened", __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. //FIXME test if MS_LOCKED
  208. if (!cs->connected) {
  209. gig_dbg(DEBUG_ANY,
  210. "can't communicate with unplugged device");
  211. retval = -ENODEV;
  212. break;
  213. }
  214. retval = copy_from_user(&buf,
  215. (const unsigned char __user *) arg, 6)
  216. ? -EFAULT : 0;
  217. if (retval >= 0) {
  218. gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
  219. 6, (const unsigned char *) arg);
  220. retval = cs->ops->brkchars(cs, buf);
  221. }
  222. break;
  223. case GIGASET_VERSION:
  224. retval = copy_from_user(version,
  225. (unsigned __user *) arg, sizeof version)
  226. ? -EFAULT : 0;
  227. if (retval >= 0)
  228. retval = if_version(cs, version);
  229. if (retval >= 0)
  230. retval = copy_to_user((unsigned __user *) arg,
  231. version, sizeof version)
  232. ? -EFAULT : 0;
  233. break;
  234. default:
  235. gig_dbg(DEBUG_ANY, "%s: arg not supported - 0x%04x",
  236. __func__, cmd);
  237. retval = -ENOIOCTLCMD;
  238. }
  239. }
  240. mutex_unlock(&cs->mutex);
  241. return retval;
  242. }
  243. static int if_tiocmget(struct tty_struct *tty, struct file *file)
  244. {
  245. struct cardstate *cs;
  246. int retval;
  247. cs = (struct cardstate *) tty->driver_data;
  248. if (!cs) {
  249. err("cs==NULL in %s", __func__);
  250. return -ENODEV;
  251. }
  252. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  253. if (mutex_lock_interruptible(&cs->mutex))
  254. return -ERESTARTSYS; // FIXME -EINTR?
  255. // FIXME read from device?
  256. retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
  257. mutex_unlock(&cs->mutex);
  258. return retval;
  259. }
  260. static int if_tiocmset(struct tty_struct *tty, struct file *file,
  261. unsigned int set, unsigned int clear)
  262. {
  263. struct cardstate *cs;
  264. int retval;
  265. unsigned mc;
  266. cs = (struct cardstate *) tty->driver_data;
  267. if (!cs) {
  268. err("cs==NULL in %s", __func__);
  269. return -ENODEV;
  270. }
  271. gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
  272. cs->minor_index, __func__, set, clear);
  273. if (mutex_lock_interruptible(&cs->mutex))
  274. return -ERESTARTSYS; // FIXME -EINTR?
  275. if (!cs->connected) {
  276. gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
  277. retval = -ENODEV;
  278. } else {
  279. mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
  280. retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
  281. cs->control_state = mc;
  282. }
  283. mutex_unlock(&cs->mutex);
  284. return retval;
  285. }
  286. static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
  287. {
  288. struct cardstate *cs;
  289. int retval = -ENODEV;
  290. cs = (struct cardstate *) tty->driver_data;
  291. if (!cs) {
  292. err("cs==NULL in %s", __func__);
  293. return -ENODEV;
  294. }
  295. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  296. if (mutex_lock_interruptible(&cs->mutex))
  297. return -ERESTARTSYS; // FIXME -EINTR?
  298. if (!cs->open_count)
  299. warn("%s: device not opened", __func__);
  300. else if (atomic_read(&cs->mstate) != MS_LOCKED) {
  301. warn("can't write to unlocked device");
  302. retval = -EBUSY;
  303. } else if (!cs->connected) {
  304. gig_dbg(DEBUG_ANY, "can't write to unplugged device");
  305. retval = -EBUSY; //FIXME
  306. } else {
  307. retval = cs->ops->write_cmd(cs, buf, count,
  308. &cs->if_wake_tasklet);
  309. }
  310. mutex_unlock(&cs->mutex);
  311. return retval;
  312. }
  313. static int if_write_room(struct tty_struct *tty)
  314. {
  315. struct cardstate *cs;
  316. int retval = -ENODEV;
  317. cs = (struct cardstate *) tty->driver_data;
  318. if (!cs) {
  319. err("cs==NULL in %s", __func__);
  320. return -ENODEV;
  321. }
  322. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  323. if (mutex_lock_interruptible(&cs->mutex))
  324. return -ERESTARTSYS; // FIXME -EINTR?
  325. if (!cs->open_count)
  326. warn("%s: device not opened", __func__);
  327. else if (atomic_read(&cs->mstate) != MS_LOCKED) {
  328. warn("can't write to unlocked device");
  329. retval = -EBUSY; //FIXME
  330. } else if (!cs->connected) {
  331. gig_dbg(DEBUG_ANY, "can't write to unplugged device");
  332. retval = -EBUSY; //FIXME
  333. } else
  334. retval = cs->ops->write_room(cs);
  335. mutex_unlock(&cs->mutex);
  336. return retval;
  337. }
  338. static int if_chars_in_buffer(struct tty_struct *tty)
  339. {
  340. struct cardstate *cs;
  341. int retval = -ENODEV;
  342. cs = (struct cardstate *) tty->driver_data;
  343. if (!cs) {
  344. err("cs==NULL in %s", __func__);
  345. return -ENODEV;
  346. }
  347. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  348. if (mutex_lock_interruptible(&cs->mutex))
  349. return -ERESTARTSYS; // FIXME -EINTR?
  350. if (!cs->open_count)
  351. warn("%s: device not opened", __func__);
  352. else if (atomic_read(&cs->mstate) != MS_LOCKED) {
  353. warn("can't write to unlocked device");
  354. retval = -EBUSY;
  355. } else if (!cs->connected) {
  356. gig_dbg(DEBUG_ANY, "can't write to unplugged device");
  357. retval = -EBUSY; //FIXME
  358. } else
  359. retval = cs->ops->chars_in_buffer(cs);
  360. mutex_unlock(&cs->mutex);
  361. return retval;
  362. }
  363. static void if_throttle(struct tty_struct *tty)
  364. {
  365. struct cardstate *cs;
  366. cs = (struct cardstate *) tty->driver_data;
  367. if (!cs) {
  368. err("cs==NULL in %s", __func__);
  369. return;
  370. }
  371. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  372. mutex_lock(&cs->mutex);
  373. if (!cs->open_count)
  374. warn("%s: device not opened", __func__);
  375. else {
  376. //FIXME
  377. }
  378. mutex_unlock(&cs->mutex);
  379. }
  380. static void if_unthrottle(struct tty_struct *tty)
  381. {
  382. struct cardstate *cs;
  383. cs = (struct cardstate *) tty->driver_data;
  384. if (!cs) {
  385. err("cs==NULL in %s", __func__);
  386. return;
  387. }
  388. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  389. mutex_lock(&cs->mutex);
  390. if (!cs->open_count)
  391. warn("%s: device not opened", __func__);
  392. else {
  393. //FIXME
  394. }
  395. mutex_unlock(&cs->mutex);
  396. }
  397. static void if_set_termios(struct tty_struct *tty, struct termios *old)
  398. {
  399. struct cardstate *cs;
  400. unsigned int iflag;
  401. unsigned int cflag;
  402. unsigned int old_cflag;
  403. unsigned int control_state, new_state;
  404. cs = (struct cardstate *) tty->driver_data;
  405. if (!cs) {
  406. err("cs==NULL in %s", __func__);
  407. return;
  408. }
  409. gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
  410. mutex_lock(&cs->mutex);
  411. if (!cs->open_count) {
  412. warn("%s: device not opened", __func__);
  413. goto out;
  414. }
  415. if (!cs->connected) {
  416. gig_dbg(DEBUG_ANY, "can't communicate with unplugged device");
  417. goto out;
  418. }
  419. // stolen from mct_u232.c
  420. iflag = tty->termios->c_iflag;
  421. cflag = tty->termios->c_cflag;
  422. old_cflag = old ? old->c_cflag : cflag; //FIXME?
  423. gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
  424. cs->minor_index, iflag, cflag, old_cflag);
  425. /* get a local copy of the current port settings */
  426. control_state = cs->control_state;
  427. /*
  428. * Update baud rate.
  429. * Do not attempt to cache old rates and skip settings,
  430. * disconnects screw such tricks up completely.
  431. * Premature optimization is the root of all evil.
  432. */
  433. /* reassert DTR and (maybe) RTS on transition from B0 */
  434. if ((old_cflag & CBAUD) == B0) {
  435. new_state = control_state | TIOCM_DTR;
  436. /* don't set RTS if using hardware flow control */
  437. if (!(old_cflag & CRTSCTS))
  438. new_state |= TIOCM_RTS;
  439. gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
  440. cs->minor_index,
  441. (new_state & TIOCM_RTS) ? " only" : "/RTS");
  442. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  443. control_state = new_state;
  444. }
  445. cs->ops->baud_rate(cs, cflag & CBAUD);
  446. if ((cflag & CBAUD) == B0) {
  447. /* Drop RTS and DTR */
  448. gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
  449. new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
  450. cs->ops->set_modem_ctrl(cs, control_state, new_state);
  451. control_state = new_state;
  452. }
  453. /*
  454. * Update line control register (LCR)
  455. */
  456. cs->ops->set_line_ctrl(cs, cflag);
  457. #if 0
  458. //FIXME this hangs M101 [ts 2005-03-09]
  459. //FIXME do we need this?
  460. /*
  461. * Set flow control: well, I do not really now how to handle DTR/RTS.
  462. * Just do what we have seen with SniffUSB on Win98.
  463. */
  464. /* Drop DTR/RTS if no flow control otherwise assert */
  465. gig_dbg(DEBUG_IF, "%u: control_state %x",
  466. cs->minor_index, control_state);
  467. new_state = control_state;
  468. if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
  469. new_state |= TIOCM_DTR | TIOCM_RTS;
  470. else
  471. new_state &= ~(TIOCM_DTR | TIOCM_RTS);
  472. if (new_state != control_state) {
  473. gig_dbg(DEBUG_IF, "%u: new_state %x",
  474. cs->minor_index, new_state);
  475. gigaset_set_modem_ctrl(cs, control_state, new_state);
  476. control_state = new_state;
  477. }
  478. #endif
  479. /* save off the modified port settings */
  480. cs->control_state = control_state;
  481. out:
  482. mutex_unlock(&cs->mutex);
  483. }
  484. /* wakeup tasklet for the write operation */
  485. static void if_wake(unsigned long data)
  486. {
  487. struct cardstate *cs = (struct cardstate *) data;
  488. struct tty_struct *tty;
  489. tty = cs->tty;
  490. if (!tty)
  491. return;
  492. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  493. tty->ldisc.write_wakeup) {
  494. gig_dbg(DEBUG_IF, "write wakeup call");
  495. tty->ldisc.write_wakeup(tty);
  496. }
  497. wake_up_interruptible(&tty->write_wait);
  498. }
  499. /*** interface to common ***/
  500. void gigaset_if_init(struct cardstate *cs)
  501. {
  502. struct gigaset_driver *drv;
  503. drv = cs->driver;
  504. if (!drv->have_tty)
  505. return;
  506. tasklet_init(&cs->if_wake_tasklet, &if_wake, (unsigned long) cs);
  507. cs->class = tty_register_device(drv->tty, cs->minor_index, NULL);
  508. if (!IS_ERR(cs->class))
  509. class_set_devdata(cs->class, cs);
  510. else {
  511. warn("could not register device to the tty subsystem");
  512. cs->class = NULL;
  513. }
  514. }
  515. void gigaset_if_free(struct cardstate *cs)
  516. {
  517. struct gigaset_driver *drv;
  518. drv = cs->driver;
  519. if (!drv->have_tty)
  520. return;
  521. tasklet_disable(&cs->if_wake_tasklet);
  522. tasklet_kill(&cs->if_wake_tasklet);
  523. cs->class = NULL;
  524. tty_unregister_device(drv->tty, cs->minor_index);
  525. }
  526. void gigaset_if_receive(struct cardstate *cs,
  527. unsigned char *buffer, size_t len)
  528. {
  529. unsigned long flags;
  530. struct tty_struct *tty;
  531. spin_lock_irqsave(&cs->lock, flags);
  532. if ((tty = cs->tty) == NULL)
  533. gig_dbg(DEBUG_ANY, "receive on closed device");
  534. else {
  535. tty_buffer_request_room(tty, len);
  536. tty_insert_flip_string(tty, buffer, len);
  537. tty_flip_buffer_push(tty);
  538. }
  539. spin_unlock_irqrestore(&cs->lock, flags);
  540. }
  541. EXPORT_SYMBOL_GPL(gigaset_if_receive);
  542. /* gigaset_if_initdriver
  543. * Initialize tty interface.
  544. * parameters:
  545. * drv Driver
  546. * procname Name of the driver (e.g. for /proc/tty/drivers)
  547. * devname Name of the device files (prefix without minor number)
  548. */
  549. void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
  550. const char *devname)
  551. {
  552. unsigned minors = drv->minors;
  553. int ret;
  554. struct tty_driver *tty;
  555. drv->have_tty = 0;
  556. if ((drv->tty = alloc_tty_driver(minors)) == NULL)
  557. goto enomem;
  558. tty = drv->tty;
  559. tty->magic = TTY_DRIVER_MAGIC,
  560. tty->major = GIG_MAJOR,
  561. tty->type = TTY_DRIVER_TYPE_SERIAL,
  562. tty->subtype = SERIAL_TYPE_NORMAL,
  563. tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  564. tty->driver_name = procname;
  565. tty->name = devname;
  566. tty->minor_start = drv->minor;
  567. tty->num = drv->minors;
  568. tty->owner = THIS_MODULE;
  569. tty->init_termios = tty_std_termios; //FIXME
  570. tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; //FIXME
  571. tty_set_operations(tty, &if_ops);
  572. ret = tty_register_driver(tty);
  573. if (ret < 0) {
  574. warn("failed to register tty driver (error %d)", ret);
  575. goto error;
  576. }
  577. gig_dbg(DEBUG_IF, "tty driver initialized");
  578. drv->have_tty = 1;
  579. return;
  580. enomem:
  581. warn("could not allocate tty structures");
  582. error:
  583. if (drv->tty)
  584. put_tty_driver(drv->tty);
  585. }
  586. void gigaset_if_freedriver(struct gigaset_driver *drv)
  587. {
  588. if (!drv->have_tty)
  589. return;
  590. drv->have_tty = 0;
  591. tty_unregister_driver(drv->tty);
  592. put_tty_driver(drv->tty);
  593. }