ctctty.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * $Id: ctctty.c,v 1.29 2005/04/05 08:50:44 mschwide Exp $
  3. *
  4. * CTC / ESCON network driver, tty interface.
  5. *
  6. * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/config.h>
  25. #include <linux/module.h>
  26. #include <linux/tty.h>
  27. #include <linux/tty_flip.h>
  28. #include <linux/serial_reg.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <asm/uaccess.h>
  32. #include <linux/devfs_fs_kernel.h>
  33. #include "ctctty.h"
  34. #include "ctcdbug.h"
  35. #define CTC_TTY_MAJOR 43
  36. #define CTC_TTY_MAX_DEVICES 64
  37. #define CTC_ASYNC_MAGIC 0x49344C01 /* for paranoia-checking */
  38. #define CTC_ASYNC_INITIALIZED 0x80000000 /* port was initialized */
  39. #define CTC_ASYNC_NORMAL_ACTIVE 0x20000000 /* Normal device active */
  40. #define CTC_ASYNC_CLOSING 0x08000000 /* Serial port is closing */
  41. #define CTC_ASYNC_CTS_FLOW 0x04000000 /* Do CTS flow control */
  42. #define CTC_ASYNC_CHECK_CD 0x02000000 /* i.e., CLOCAL */
  43. #define CTC_ASYNC_HUP_NOTIFY 0x0001 /* Notify tty on hangups/closes */
  44. #define CTC_ASYNC_NETDEV_OPEN 0x0002 /* Underlying netdev is open */
  45. #define CTC_ASYNC_TX_LINESTAT 0x0004 /* Must send line status */
  46. #define CTC_ASYNC_SPLIT_TERMIOS 0x0008 /* Sep. termios for dialin/out */
  47. #define CTC_TTY_XMIT_SIZE 1024 /* Default bufsize for write */
  48. #define CTC_SERIAL_XMIT_MAX 4000 /* Maximum bufsize for write */
  49. /* Private data (similar to async_struct in <linux/serial.h>) */
  50. typedef struct {
  51. int magic;
  52. int flags; /* defined in tty.h */
  53. int mcr; /* Modem control register */
  54. int msr; /* Modem status register */
  55. int lsr; /* Line status register */
  56. int line;
  57. int count; /* # of fd on device */
  58. int blocked_open; /* # of blocked opens */
  59. struct net_device *netdev;
  60. struct sk_buff_head tx_queue; /* transmit queue */
  61. struct sk_buff_head rx_queue; /* receive queue */
  62. struct tty_struct *tty; /* Pointer to corresponding tty */
  63. wait_queue_head_t open_wait;
  64. wait_queue_head_t close_wait;
  65. struct semaphore write_sem;
  66. struct tasklet_struct tasklet;
  67. struct timer_list stoptimer;
  68. } ctc_tty_info;
  69. /* Description of one CTC-tty */
  70. typedef struct {
  71. struct tty_driver *ctc_tty_device; /* tty-device */
  72. ctc_tty_info info[CTC_TTY_MAX_DEVICES]; /* Private data */
  73. } ctc_tty_driver;
  74. static ctc_tty_driver *driver;
  75. /* Leave this unchanged unless you know what you do! */
  76. #define MODEM_PARANOIA_CHECK
  77. #define MODEM_DO_RESTART
  78. #define CTC_TTY_NAME "ctctty"
  79. static __u32 ctc_tty_magic = CTC_ASYNC_MAGIC;
  80. static int ctc_tty_shuttingdown = 0;
  81. static spinlock_t ctc_tty_lock;
  82. /* ctc_tty_try_read() is called from within ctc_tty_rcv_skb()
  83. * to stuff incoming data directly into a tty's flip-buffer. If the
  84. * flip buffer is full, the packet gets queued up.
  85. *
  86. * Return:
  87. * 1 = Success
  88. * 0 = Failure, data has to be buffered and later processed by
  89. * ctc_tty_readmodem().
  90. */
  91. static int
  92. ctc_tty_try_read(ctc_tty_info * info, struct sk_buff *skb)
  93. {
  94. int len;
  95. struct tty_struct *tty;
  96. DBF_TEXT(trace, 5, __FUNCTION__);
  97. if ((tty = info->tty)) {
  98. if (info->mcr & UART_MCR_RTS) {
  99. len = skb->len;
  100. tty_insert_flip_string(tty, skb->data, len);
  101. tty_flip_buffer_push(tty);
  102. kfree_skb(skb);
  103. return 1;
  104. }
  105. }
  106. return 0;
  107. }
  108. /* ctc_tty_readmodem() is called periodically from within timer-interrupt.
  109. * It tries getting received data from the receive queue an stuff it into
  110. * the tty's flip-buffer.
  111. */
  112. static int
  113. ctc_tty_readmodem(ctc_tty_info *info)
  114. {
  115. int ret = 1;
  116. struct tty_struct *tty;
  117. DBF_TEXT(trace, 5, __FUNCTION__);
  118. if ((tty = info->tty)) {
  119. if (info->mcr & UART_MCR_RTS) {
  120. struct sk_buff *skb;
  121. if ((skb = skb_dequeue(&info->rx_queue))) {
  122. int len = skb->len;
  123. tty_insert_flip_string(tty, skb->data, len);
  124. skb_pull(skb, len);
  125. tty_flip_buffer_push(tty);
  126. if (skb->len > 0)
  127. skb_queue_head(&info->rx_queue, skb);
  128. else {
  129. kfree_skb(skb);
  130. ret = !skb_queue_empty(&info->rx_queue);
  131. }
  132. }
  133. }
  134. }
  135. return ret;
  136. }
  137. void
  138. ctc_tty_setcarrier(struct net_device *netdev, int on)
  139. {
  140. int i;
  141. DBF_TEXT(trace, 4, __FUNCTION__);
  142. if ((!driver) || ctc_tty_shuttingdown)
  143. return;
  144. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
  145. if (driver->info[i].netdev == netdev) {
  146. ctc_tty_info *info = &driver->info[i];
  147. if (on)
  148. info->msr |= UART_MSR_DCD;
  149. else
  150. info->msr &= ~UART_MSR_DCD;
  151. if ((info->flags & CTC_ASYNC_CHECK_CD) && (!on))
  152. tty_hangup(info->tty);
  153. }
  154. }
  155. void
  156. ctc_tty_netif_rx(struct sk_buff *skb)
  157. {
  158. int i;
  159. ctc_tty_info *info = NULL;
  160. DBF_TEXT(trace, 5, __FUNCTION__);
  161. if (!skb)
  162. return;
  163. if ((!skb->dev) || (!driver) || ctc_tty_shuttingdown) {
  164. dev_kfree_skb(skb);
  165. return;
  166. }
  167. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
  168. if (driver->info[i].netdev == skb->dev) {
  169. info = &driver->info[i];
  170. break;
  171. }
  172. if (!info) {
  173. dev_kfree_skb(skb);
  174. return;
  175. }
  176. if (skb->len < 6) {
  177. dev_kfree_skb(skb);
  178. return;
  179. }
  180. if (memcmp(skb->data, &ctc_tty_magic, sizeof(__u32))) {
  181. dev_kfree_skb(skb);
  182. return;
  183. }
  184. skb_pull(skb, sizeof(__u32));
  185. i = *((int *)skb->data);
  186. skb_pull(skb, sizeof(info->mcr));
  187. if (i & UART_MCR_RTS) {
  188. info->msr |= UART_MSR_CTS;
  189. if (info->flags & CTC_ASYNC_CTS_FLOW)
  190. info->tty->hw_stopped = 0;
  191. } else {
  192. info->msr &= ~UART_MSR_CTS;
  193. if (info->flags & CTC_ASYNC_CTS_FLOW)
  194. info->tty->hw_stopped = 1;
  195. }
  196. if (i & UART_MCR_DTR)
  197. info->msr |= UART_MSR_DSR;
  198. else
  199. info->msr &= ~UART_MSR_DSR;
  200. if (skb->len <= 0) {
  201. kfree_skb(skb);
  202. return;
  203. }
  204. /* Try to deliver directly via tty-flip-buf if queue is empty */
  205. if (skb_queue_empty(&info->rx_queue))
  206. if (ctc_tty_try_read(info, skb))
  207. return;
  208. /* Direct deliver failed or queue wasn't empty.
  209. * Queue up for later dequeueing via timer-irq.
  210. */
  211. skb_queue_tail(&info->rx_queue, skb);
  212. /* Schedule dequeuing */
  213. tasklet_schedule(&info->tasklet);
  214. }
  215. static int
  216. ctc_tty_tint(ctc_tty_info * info)
  217. {
  218. struct sk_buff *skb = skb_dequeue(&info->tx_queue);
  219. int stopped = (info->tty->hw_stopped || info->tty->stopped);
  220. int wake = 1;
  221. int rc;
  222. DBF_TEXT(trace, 4, __FUNCTION__);
  223. if (!info->netdev) {
  224. if (skb)
  225. kfree_skb(skb);
  226. return 0;
  227. }
  228. if (info->flags & CTC_ASYNC_TX_LINESTAT) {
  229. int skb_res = info->netdev->hard_header_len +
  230. sizeof(info->mcr) + sizeof(__u32);
  231. /* If we must update line status,
  232. * create an empty dummy skb and insert it.
  233. */
  234. if (skb)
  235. skb_queue_head(&info->tx_queue, skb);
  236. skb = dev_alloc_skb(skb_res);
  237. if (!skb) {
  238. printk(KERN_WARNING
  239. "ctc_tty: Out of memory in %s%d tint\n",
  240. CTC_TTY_NAME, info->line);
  241. return 1;
  242. }
  243. skb_reserve(skb, skb_res);
  244. stopped = 0;
  245. wake = 0;
  246. }
  247. if (!skb)
  248. return 0;
  249. if (stopped) {
  250. skb_queue_head(&info->tx_queue, skb);
  251. return 1;
  252. }
  253. #if 0
  254. if (skb->len > 0)
  255. printk(KERN_DEBUG "tint: %d %02x\n", skb->len, *(skb->data));
  256. else
  257. printk(KERN_DEBUG "tint: %d STAT\n", skb->len);
  258. #endif
  259. memcpy(skb_push(skb, sizeof(info->mcr)), &info->mcr, sizeof(info->mcr));
  260. memcpy(skb_push(skb, sizeof(__u32)), &ctc_tty_magic, sizeof(__u32));
  261. rc = info->netdev->hard_start_xmit(skb, info->netdev);
  262. if (rc) {
  263. skb_pull(skb, sizeof(info->mcr) + sizeof(__u32));
  264. if (skb->len > 0)
  265. skb_queue_head(&info->tx_queue, skb);
  266. else
  267. kfree_skb(skb);
  268. } else {
  269. struct tty_struct *tty = info->tty;
  270. info->flags &= ~CTC_ASYNC_TX_LINESTAT;
  271. if (tty) {
  272. tty_wakeup(tty);
  273. }
  274. }
  275. return (skb_queue_empty(&info->tx_queue) ? 0 : 1);
  276. }
  277. /************************************************************
  278. *
  279. * Modem-functions
  280. *
  281. * mostly "stolen" from original Linux-serial.c and friends.
  282. *
  283. ************************************************************/
  284. static inline int
  285. ctc_tty_paranoia_check(ctc_tty_info * info, char *name, const char *routine)
  286. {
  287. #ifdef MODEM_PARANOIA_CHECK
  288. if (!info) {
  289. printk(KERN_WARNING "ctc_tty: null info_struct for %s in %s\n",
  290. name, routine);
  291. return 1;
  292. }
  293. if (info->magic != CTC_ASYNC_MAGIC) {
  294. printk(KERN_WARNING "ctc_tty: bad magic for info struct %s in %s\n",
  295. name, routine);
  296. return 1;
  297. }
  298. #endif
  299. return 0;
  300. }
  301. static void
  302. ctc_tty_inject(ctc_tty_info *info, char c)
  303. {
  304. int skb_res;
  305. struct sk_buff *skb;
  306. DBF_TEXT(trace, 4, __FUNCTION__);
  307. if (ctc_tty_shuttingdown)
  308. return;
  309. skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
  310. sizeof(__u32) + 1;
  311. skb = dev_alloc_skb(skb_res);
  312. if (!skb) {
  313. printk(KERN_WARNING
  314. "ctc_tty: Out of memory in %s%d tx_inject\n",
  315. CTC_TTY_NAME, info->line);
  316. return;
  317. }
  318. skb_reserve(skb, skb_res);
  319. *(skb_put(skb, 1)) = c;
  320. skb_queue_head(&info->tx_queue, skb);
  321. tasklet_schedule(&info->tasklet);
  322. }
  323. static void
  324. ctc_tty_transmit_status(ctc_tty_info *info)
  325. {
  326. DBF_TEXT(trace, 5, __FUNCTION__);
  327. if (ctc_tty_shuttingdown)
  328. return;
  329. info->flags |= CTC_ASYNC_TX_LINESTAT;
  330. tasklet_schedule(&info->tasklet);
  331. }
  332. static void
  333. ctc_tty_change_speed(ctc_tty_info * info)
  334. {
  335. unsigned int cflag;
  336. unsigned int quot;
  337. int i;
  338. DBF_TEXT(trace, 3, __FUNCTION__);
  339. if (!info->tty || !info->tty->termios)
  340. return;
  341. cflag = info->tty->termios->c_cflag;
  342. quot = i = cflag & CBAUD;
  343. if (i & CBAUDEX) {
  344. i &= ~CBAUDEX;
  345. if (i < 1 || i > 2)
  346. info->tty->termios->c_cflag &= ~CBAUDEX;
  347. else
  348. i += 15;
  349. }
  350. if (quot) {
  351. info->mcr |= UART_MCR_DTR;
  352. info->mcr |= UART_MCR_RTS;
  353. ctc_tty_transmit_status(info);
  354. } else {
  355. info->mcr &= ~UART_MCR_DTR;
  356. info->mcr &= ~UART_MCR_RTS;
  357. ctc_tty_transmit_status(info);
  358. return;
  359. }
  360. /* CTS flow control flag and modem status interrupts */
  361. if (cflag & CRTSCTS) {
  362. info->flags |= CTC_ASYNC_CTS_FLOW;
  363. } else
  364. info->flags &= ~CTC_ASYNC_CTS_FLOW;
  365. if (cflag & CLOCAL)
  366. info->flags &= ~CTC_ASYNC_CHECK_CD;
  367. else {
  368. info->flags |= CTC_ASYNC_CHECK_CD;
  369. }
  370. }
  371. static int
  372. ctc_tty_startup(ctc_tty_info * info)
  373. {
  374. DBF_TEXT(trace, 3, __FUNCTION__);
  375. if (info->flags & CTC_ASYNC_INITIALIZED)
  376. return 0;
  377. #ifdef CTC_DEBUG_MODEM_OPEN
  378. printk(KERN_DEBUG "starting up %s%d ...\n", CTC_TTY_NAME, info->line);
  379. #endif
  380. /*
  381. * Now, initialize the UART
  382. */
  383. info->mcr = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
  384. if (info->tty)
  385. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  386. /*
  387. * and set the speed of the serial port
  388. */
  389. ctc_tty_change_speed(info);
  390. info->flags |= CTC_ASYNC_INITIALIZED;
  391. if (!(info->flags & CTC_ASYNC_NETDEV_OPEN))
  392. info->netdev->open(info->netdev);
  393. info->flags |= CTC_ASYNC_NETDEV_OPEN;
  394. return 0;
  395. }
  396. static void
  397. ctc_tty_stopdev(unsigned long data)
  398. {
  399. ctc_tty_info *info = (ctc_tty_info *)data;
  400. if ((!info) || (!info->netdev) ||
  401. (info->flags & CTC_ASYNC_INITIALIZED))
  402. return;
  403. info->netdev->stop(info->netdev);
  404. info->flags &= ~CTC_ASYNC_NETDEV_OPEN;
  405. }
  406. /*
  407. * This routine will shutdown a serial port; interrupts are disabled, and
  408. * DTR is dropped if the hangup on close termio flag is on.
  409. */
  410. static void
  411. ctc_tty_shutdown(ctc_tty_info * info)
  412. {
  413. DBF_TEXT(trace, 3, __FUNCTION__);
  414. if (!(info->flags & CTC_ASYNC_INITIALIZED))
  415. return;
  416. #ifdef CTC_DEBUG_MODEM_OPEN
  417. printk(KERN_DEBUG "Shutting down %s%d ....\n", CTC_TTY_NAME, info->line);
  418. #endif
  419. info->msr &= ~UART_MSR_RI;
  420. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  421. info->mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
  422. if (info->tty)
  423. set_bit(TTY_IO_ERROR, &info->tty->flags);
  424. mod_timer(&info->stoptimer, jiffies + (10 * HZ));
  425. skb_queue_purge(&info->tx_queue);
  426. skb_queue_purge(&info->rx_queue);
  427. info->flags &= ~CTC_ASYNC_INITIALIZED;
  428. }
  429. /* ctc_tty_write() is the main send-routine. It is called from the upper
  430. * levels within the kernel to perform sending data. Depending on the
  431. * online-flag it either directs output to the at-command-interpreter or
  432. * to the lower level. Additional tasks done here:
  433. * - If online, check for escape-sequence (+++)
  434. * - If sending audio-data, call ctc_tty_DLEdown() to parse DLE-codes.
  435. * - If receiving audio-data, call ctc_tty_end_vrx() to abort if needed.
  436. * - If dialing, abort dial.
  437. */
  438. static int
  439. ctc_tty_write(struct tty_struct *tty, const u_char * buf, int count)
  440. {
  441. int c;
  442. int total = 0;
  443. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  444. DBF_TEXT(trace, 5, __FUNCTION__);
  445. if (ctc_tty_shuttingdown)
  446. goto ex;
  447. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write"))
  448. goto ex;
  449. if (!tty)
  450. goto ex;
  451. if (!info->netdev) {
  452. total = -ENODEV;
  453. goto ex;
  454. }
  455. while (1) {
  456. struct sk_buff *skb;
  457. int skb_res;
  458. c = (count < CTC_TTY_XMIT_SIZE) ? count : CTC_TTY_XMIT_SIZE;
  459. if (c <= 0)
  460. break;
  461. skb_res = info->netdev->hard_header_len + sizeof(info->mcr) +
  462. + sizeof(__u32);
  463. skb = dev_alloc_skb(skb_res + c);
  464. if (!skb) {
  465. printk(KERN_WARNING
  466. "ctc_tty: Out of memory in %s%d write\n",
  467. CTC_TTY_NAME, info->line);
  468. break;
  469. }
  470. skb_reserve(skb, skb_res);
  471. memcpy(skb_put(skb, c), buf, c);
  472. skb_queue_tail(&info->tx_queue, skb);
  473. buf += c;
  474. total += c;
  475. count -= c;
  476. }
  477. if (!skb_queue_empty(&info->tx_queue)) {
  478. info->lsr &= ~UART_LSR_TEMT;
  479. tasklet_schedule(&info->tasklet);
  480. }
  481. ex:
  482. DBF_TEXT(trace, 6, __FUNCTION__);
  483. return total;
  484. }
  485. static int
  486. ctc_tty_write_room(struct tty_struct *tty)
  487. {
  488. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  489. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_write_room"))
  490. return 0;
  491. return CTC_TTY_XMIT_SIZE;
  492. }
  493. static int
  494. ctc_tty_chars_in_buffer(struct tty_struct *tty)
  495. {
  496. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  497. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_chars_in_buffer"))
  498. return 0;
  499. return 0;
  500. }
  501. static void
  502. ctc_tty_flush_buffer(struct tty_struct *tty)
  503. {
  504. ctc_tty_info *info;
  505. unsigned long flags;
  506. DBF_TEXT(trace, 4, __FUNCTION__);
  507. if (!tty)
  508. goto ex;
  509. spin_lock_irqsave(&ctc_tty_lock, flags);
  510. info = (ctc_tty_info *) tty->driver_data;
  511. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_buffer")) {
  512. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  513. goto ex;
  514. }
  515. skb_queue_purge(&info->tx_queue);
  516. info->lsr |= UART_LSR_TEMT;
  517. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  518. wake_up_interruptible(&tty->write_wait);
  519. tty_wakeup(tty);
  520. ex:
  521. DBF_TEXT_(trace, 2, "ex: %s ", __FUNCTION__);
  522. return;
  523. }
  524. static void
  525. ctc_tty_flush_chars(struct tty_struct *tty)
  526. {
  527. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  528. DBF_TEXT(trace, 4, __FUNCTION__);
  529. if (ctc_tty_shuttingdown)
  530. return;
  531. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_flush_chars"))
  532. return;
  533. if (tty->stopped || tty->hw_stopped || skb_queue_empty(&info->tx_queue))
  534. return;
  535. tasklet_schedule(&info->tasklet);
  536. }
  537. /*
  538. * ------------------------------------------------------------
  539. * ctc_tty_throttle()
  540. *
  541. * This routine is called by the upper-layer tty layer to signal that
  542. * incoming characters should be throttled.
  543. * ------------------------------------------------------------
  544. */
  545. static void
  546. ctc_tty_throttle(struct tty_struct *tty)
  547. {
  548. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  549. DBF_TEXT(trace, 4, __FUNCTION__);
  550. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_throttle"))
  551. return;
  552. info->mcr &= ~UART_MCR_RTS;
  553. if (I_IXOFF(tty))
  554. ctc_tty_inject(info, STOP_CHAR(tty));
  555. ctc_tty_transmit_status(info);
  556. }
  557. static void
  558. ctc_tty_unthrottle(struct tty_struct *tty)
  559. {
  560. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  561. DBF_TEXT(trace, 4, __FUNCTION__);
  562. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_unthrottle"))
  563. return;
  564. info->mcr |= UART_MCR_RTS;
  565. if (I_IXOFF(tty))
  566. ctc_tty_inject(info, START_CHAR(tty));
  567. ctc_tty_transmit_status(info);
  568. }
  569. /*
  570. * ------------------------------------------------------------
  571. * ctc_tty_ioctl() and friends
  572. * ------------------------------------------------------------
  573. */
  574. /*
  575. * ctc_tty_get_lsr_info - get line status register info
  576. *
  577. * Purpose: Let user call ioctl() to get info when the UART physically
  578. * is emptied. On bus types like RS485, the transmitter must
  579. * release the bus after transmitting. This must be done when
  580. * the transmit shift register is empty, not be done when the
  581. * transmit holding register is empty. This functionality
  582. * allows RS485 driver to be written in user space.
  583. */
  584. static int
  585. ctc_tty_get_lsr_info(ctc_tty_info * info, uint __user *value)
  586. {
  587. u_char status;
  588. uint result;
  589. ulong flags;
  590. DBF_TEXT(trace, 4, __FUNCTION__);
  591. spin_lock_irqsave(&ctc_tty_lock, flags);
  592. status = info->lsr;
  593. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  594. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  595. put_user(result, value);
  596. return 0;
  597. }
  598. static int ctc_tty_tiocmget(struct tty_struct *tty, struct file *file)
  599. {
  600. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  601. u_char control,
  602. status;
  603. uint result;
  604. ulong flags;
  605. DBF_TEXT(trace, 4, __FUNCTION__);
  606. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
  607. return -ENODEV;
  608. if (tty->flags & (1 << TTY_IO_ERROR))
  609. return -EIO;
  610. control = info->mcr;
  611. spin_lock_irqsave(&ctc_tty_lock, flags);
  612. status = info->msr;
  613. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  614. result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
  615. | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
  616. | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
  617. | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
  618. | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
  619. | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  620. return result;
  621. }
  622. static int
  623. ctc_tty_tiocmset(struct tty_struct *tty, struct file *file,
  624. unsigned int set, unsigned int clear)
  625. {
  626. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  627. DBF_TEXT(trace, 4, __FUNCTION__);
  628. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
  629. return -ENODEV;
  630. if (tty->flags & (1 << TTY_IO_ERROR))
  631. return -EIO;
  632. if (set & TIOCM_RTS)
  633. info->mcr |= UART_MCR_RTS;
  634. if (set & TIOCM_DTR)
  635. info->mcr |= UART_MCR_DTR;
  636. if (clear & TIOCM_RTS)
  637. info->mcr &= ~UART_MCR_RTS;
  638. if (clear & TIOCM_DTR)
  639. info->mcr &= ~UART_MCR_DTR;
  640. if ((set | clear) & (TIOCM_RTS|TIOCM_DTR))
  641. ctc_tty_transmit_status(info);
  642. return 0;
  643. }
  644. static int
  645. ctc_tty_ioctl(struct tty_struct *tty, struct file *file,
  646. uint cmd, ulong arg)
  647. {
  648. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  649. int error;
  650. int retval;
  651. DBF_TEXT(trace, 4, __FUNCTION__);
  652. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_ioctl"))
  653. return -ENODEV;
  654. if (tty->flags & (1 << TTY_IO_ERROR))
  655. return -EIO;
  656. switch (cmd) {
  657. case TCSBRK: /* SVID version: non-zero arg --> no break */
  658. #ifdef CTC_DEBUG_MODEM_IOCTL
  659. printk(KERN_DEBUG "%s%d ioctl TCSBRK\n", CTC_TTY_NAME, info->line);
  660. #endif
  661. retval = tty_check_change(tty);
  662. if (retval)
  663. return retval;
  664. tty_wait_until_sent(tty, 0);
  665. return 0;
  666. case TCSBRKP: /* support for POSIX tcsendbreak() */
  667. #ifdef CTC_DEBUG_MODEM_IOCTL
  668. printk(KERN_DEBUG "%s%d ioctl TCSBRKP\n", CTC_TTY_NAME, info->line);
  669. #endif
  670. retval = tty_check_change(tty);
  671. if (retval)
  672. return retval;
  673. tty_wait_until_sent(tty, 0);
  674. return 0;
  675. case TIOCGSOFTCAR:
  676. #ifdef CTC_DEBUG_MODEM_IOCTL
  677. printk(KERN_DEBUG "%s%d ioctl TIOCGSOFTCAR\n", CTC_TTY_NAME,
  678. info->line);
  679. #endif
  680. error = put_user(C_CLOCAL(tty) ? 1 : 0, (ulong __user *) arg);
  681. return error;
  682. case TIOCSSOFTCAR:
  683. #ifdef CTC_DEBUG_MODEM_IOCTL
  684. printk(KERN_DEBUG "%s%d ioctl TIOCSSOFTCAR\n", CTC_TTY_NAME,
  685. info->line);
  686. #endif
  687. error = get_user(arg, (ulong __user *) arg);
  688. if (error)
  689. return error;
  690. tty->termios->c_cflag =
  691. ((tty->termios->c_cflag & ~CLOCAL) |
  692. (arg ? CLOCAL : 0));
  693. return 0;
  694. case TIOCSERGETLSR: /* Get line status register */
  695. #ifdef CTC_DEBUG_MODEM_IOCTL
  696. printk(KERN_DEBUG "%s%d ioctl TIOCSERGETLSR\n", CTC_TTY_NAME,
  697. info->line);
  698. #endif
  699. if (access_ok(VERIFY_WRITE, (void __user *) arg, sizeof(uint)))
  700. return ctc_tty_get_lsr_info(info, (uint __user *) arg);
  701. else
  702. return -EFAULT;
  703. default:
  704. #ifdef CTC_DEBUG_MODEM_IOCTL
  705. printk(KERN_DEBUG "UNKNOWN ioctl 0x%08x on %s%d\n", cmd,
  706. CTC_TTY_NAME, info->line);
  707. #endif
  708. return -ENOIOCTLCMD;
  709. }
  710. return 0;
  711. }
  712. static void
  713. ctc_tty_set_termios(struct tty_struct *tty, struct termios *old_termios)
  714. {
  715. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  716. unsigned int cflag = tty->termios->c_cflag;
  717. DBF_TEXT(trace, 4, __FUNCTION__);
  718. ctc_tty_change_speed(info);
  719. /* Handle transition to B0 */
  720. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
  721. info->mcr &= ~(UART_MCR_DTR|UART_MCR_RTS);
  722. ctc_tty_transmit_status(info);
  723. }
  724. /* Handle transition from B0 to other */
  725. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  726. info->mcr |= UART_MCR_DTR;
  727. if (!(tty->termios->c_cflag & CRTSCTS) ||
  728. !test_bit(TTY_THROTTLED, &tty->flags)) {
  729. info->mcr |= UART_MCR_RTS;
  730. }
  731. ctc_tty_transmit_status(info);
  732. }
  733. /* Handle turning off CRTSCTS */
  734. if ((old_termios->c_cflag & CRTSCTS) &&
  735. !(tty->termios->c_cflag & CRTSCTS))
  736. tty->hw_stopped = 0;
  737. }
  738. /*
  739. * ------------------------------------------------------------
  740. * ctc_tty_open() and friends
  741. * ------------------------------------------------------------
  742. */
  743. static int
  744. ctc_tty_block_til_ready(struct tty_struct *tty, struct file *filp, ctc_tty_info *info)
  745. {
  746. DECLARE_WAITQUEUE(wait, NULL);
  747. int do_clocal = 0;
  748. unsigned long flags;
  749. int retval;
  750. DBF_TEXT(trace, 4, __FUNCTION__);
  751. /*
  752. * If the device is in the middle of being closed, then block
  753. * until it's done, and then try again.
  754. */
  755. if (tty_hung_up_p(filp) ||
  756. (info->flags & CTC_ASYNC_CLOSING)) {
  757. if (info->flags & CTC_ASYNC_CLOSING)
  758. wait_event(info->close_wait,
  759. !(info->flags & CTC_ASYNC_CLOSING));
  760. #ifdef MODEM_DO_RESTART
  761. if (info->flags & CTC_ASYNC_HUP_NOTIFY)
  762. return -EAGAIN;
  763. else
  764. return -ERESTARTSYS;
  765. #else
  766. return -EAGAIN;
  767. #endif
  768. }
  769. /*
  770. * If non-blocking mode is set, then make the check up front
  771. * and then exit.
  772. */
  773. if ((filp->f_flags & O_NONBLOCK) ||
  774. (tty->flags & (1 << TTY_IO_ERROR))) {
  775. info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
  776. return 0;
  777. }
  778. if (tty->termios->c_cflag & CLOCAL)
  779. do_clocal = 1;
  780. /*
  781. * Block waiting for the carrier detect and the line to become
  782. * free (i.e., not in use by the callout). While we are in
  783. * this loop, info->count is dropped by one, so that
  784. * ctc_tty_close() knows when to free things. We restore it upon
  785. * exit, either normal or abnormal.
  786. */
  787. retval = 0;
  788. add_wait_queue(&info->open_wait, &wait);
  789. #ifdef CTC_DEBUG_MODEM_OPEN
  790. printk(KERN_DEBUG "ctc_tty_block_til_ready before block: %s%d, count = %d\n",
  791. CTC_TTY_NAME, info->line, info->count);
  792. #endif
  793. spin_lock_irqsave(&ctc_tty_lock, flags);
  794. if (!(tty_hung_up_p(filp)))
  795. info->count--;
  796. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  797. info->blocked_open++;
  798. while (1) {
  799. set_current_state(TASK_INTERRUPTIBLE);
  800. if (tty_hung_up_p(filp) ||
  801. !(info->flags & CTC_ASYNC_INITIALIZED)) {
  802. #ifdef MODEM_DO_RESTART
  803. if (info->flags & CTC_ASYNC_HUP_NOTIFY)
  804. retval = -EAGAIN;
  805. else
  806. retval = -ERESTARTSYS;
  807. #else
  808. retval = -EAGAIN;
  809. #endif
  810. break;
  811. }
  812. if (!(info->flags & CTC_ASYNC_CLOSING) &&
  813. (do_clocal || (info->msr & UART_MSR_DCD))) {
  814. break;
  815. }
  816. if (signal_pending(current)) {
  817. retval = -ERESTARTSYS;
  818. break;
  819. }
  820. #ifdef CTC_DEBUG_MODEM_OPEN
  821. printk(KERN_DEBUG "ctc_tty_block_til_ready blocking: %s%d, count = %d\n",
  822. CTC_TTY_NAME, info->line, info->count);
  823. #endif
  824. schedule();
  825. }
  826. current->state = TASK_RUNNING;
  827. remove_wait_queue(&info->open_wait, &wait);
  828. if (!tty_hung_up_p(filp))
  829. info->count++;
  830. info->blocked_open--;
  831. #ifdef CTC_DEBUG_MODEM_OPEN
  832. printk(KERN_DEBUG "ctc_tty_block_til_ready after blocking: %s%d, count = %d\n",
  833. CTC_TTY_NAME, info->line, info->count);
  834. #endif
  835. if (retval)
  836. return retval;
  837. info->flags |= CTC_ASYNC_NORMAL_ACTIVE;
  838. return 0;
  839. }
  840. /*
  841. * This routine is called whenever a serial port is opened. It
  842. * enables interrupts for a serial port, linking in its async structure into
  843. * the IRQ chain. It also performs the serial-specific
  844. * initialization for the tty structure.
  845. */
  846. static int
  847. ctc_tty_open(struct tty_struct *tty, struct file *filp)
  848. {
  849. ctc_tty_info *info;
  850. unsigned long saveflags;
  851. int retval,
  852. line;
  853. DBF_TEXT(trace, 3, __FUNCTION__);
  854. line = tty->index;
  855. if (line < 0 || line > CTC_TTY_MAX_DEVICES)
  856. return -ENODEV;
  857. info = &driver->info[line];
  858. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_open"))
  859. return -ENODEV;
  860. if (!info->netdev)
  861. return -ENODEV;
  862. #ifdef CTC_DEBUG_MODEM_OPEN
  863. printk(KERN_DEBUG "ctc_tty_open %s, count = %d\n", tty->name,
  864. info->count);
  865. #endif
  866. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  867. info->count++;
  868. tty->driver_data = info;
  869. info->tty = tty;
  870. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  871. /*
  872. * Start up serial port
  873. */
  874. retval = ctc_tty_startup(info);
  875. if (retval) {
  876. #ifdef CTC_DEBUG_MODEM_OPEN
  877. printk(KERN_DEBUG "ctc_tty_open return after startup\n");
  878. #endif
  879. return retval;
  880. }
  881. retval = ctc_tty_block_til_ready(tty, filp, info);
  882. if (retval) {
  883. #ifdef CTC_DEBUG_MODEM_OPEN
  884. printk(KERN_DEBUG "ctc_tty_open return after ctc_tty_block_til_ready \n");
  885. #endif
  886. return retval;
  887. }
  888. #ifdef CTC_DEBUG_MODEM_OPEN
  889. printk(KERN_DEBUG "ctc_tty_open %s successful...\n", tty->name);
  890. #endif
  891. return 0;
  892. }
  893. static void
  894. ctc_tty_close(struct tty_struct *tty, struct file *filp)
  895. {
  896. ctc_tty_info *info = (ctc_tty_info *) tty->driver_data;
  897. ulong flags;
  898. ulong timeout;
  899. DBF_TEXT(trace, 3, __FUNCTION__);
  900. if (!info || ctc_tty_paranoia_check(info, tty->name, "ctc_tty_close"))
  901. return;
  902. spin_lock_irqsave(&ctc_tty_lock, flags);
  903. if (tty_hung_up_p(filp)) {
  904. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  905. #ifdef CTC_DEBUG_MODEM_OPEN
  906. printk(KERN_DEBUG "ctc_tty_close return after tty_hung_up_p\n");
  907. #endif
  908. return;
  909. }
  910. if ((tty->count == 1) && (info->count != 1)) {
  911. /*
  912. * Uh, oh. tty->count is 1, which means that the tty
  913. * structure will be freed. Info->count should always
  914. * be one in these conditions. If it's greater than
  915. * one, we've got real problems, since it means the
  916. * serial port won't be shutdown.
  917. */
  918. printk(KERN_ERR "ctc_tty_close: bad port count; tty->count is 1, "
  919. "info->count is %d\n", info->count);
  920. info->count = 1;
  921. }
  922. if (--info->count < 0) {
  923. printk(KERN_ERR "ctc_tty_close: bad port count for %s%d: %d\n",
  924. CTC_TTY_NAME, info->line, info->count);
  925. info->count = 0;
  926. }
  927. if (info->count) {
  928. local_irq_restore(flags);
  929. #ifdef CTC_DEBUG_MODEM_OPEN
  930. printk(KERN_DEBUG "ctc_tty_close after info->count != 0\n");
  931. #endif
  932. return;
  933. }
  934. info->flags |= CTC_ASYNC_CLOSING;
  935. tty->closing = 1;
  936. /*
  937. * At this point we stop accepting input. To do this, we
  938. * disable the receive line status interrupts, and tell the
  939. * interrupt driver to stop checking the data ready bit in the
  940. * line status register.
  941. */
  942. if (info->flags & CTC_ASYNC_INITIALIZED) {
  943. tty_wait_until_sent(tty, 30*HZ); /* 30 seconds timeout */
  944. /*
  945. * Before we drop DTR, make sure the UART transmitter
  946. * has completely drained; this is especially
  947. * important if there is a transmit FIFO!
  948. */
  949. timeout = jiffies + HZ;
  950. while (!(info->lsr & UART_LSR_TEMT)) {
  951. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  952. msleep(500);
  953. spin_lock_irqsave(&ctc_tty_lock, flags);
  954. if (time_after(jiffies,timeout))
  955. break;
  956. }
  957. }
  958. ctc_tty_shutdown(info);
  959. if (tty->driver->flush_buffer) {
  960. skb_queue_purge(&info->tx_queue);
  961. info->lsr |= UART_LSR_TEMT;
  962. }
  963. tty_ldisc_flush(tty);
  964. info->tty = 0;
  965. tty->closing = 0;
  966. if (info->blocked_open) {
  967. msleep_interruptible(500);
  968. wake_up_interruptible(&info->open_wait);
  969. }
  970. info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
  971. wake_up_interruptible(&info->close_wait);
  972. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  973. #ifdef CTC_DEBUG_MODEM_OPEN
  974. printk(KERN_DEBUG "ctc_tty_close normal exit\n");
  975. #endif
  976. }
  977. /*
  978. * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
  979. */
  980. static void
  981. ctc_tty_hangup(struct tty_struct *tty)
  982. {
  983. ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
  984. unsigned long saveflags;
  985. DBF_TEXT(trace, 3, __FUNCTION__);
  986. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
  987. return;
  988. ctc_tty_shutdown(info);
  989. info->count = 0;
  990. info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
  991. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  992. info->tty = 0;
  993. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  994. wake_up_interruptible(&info->open_wait);
  995. }
  996. /*
  997. * For all online tty's, try sending data to
  998. * the lower levels.
  999. */
  1000. static void
  1001. ctc_tty_task(unsigned long arg)
  1002. {
  1003. ctc_tty_info *info = (void *)arg;
  1004. unsigned long saveflags;
  1005. int again;
  1006. DBF_TEXT(trace, 3, __FUNCTION__);
  1007. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1008. if ((!ctc_tty_shuttingdown) && info) {
  1009. again = ctc_tty_tint(info);
  1010. if (!again)
  1011. info->lsr |= UART_LSR_TEMT;
  1012. again |= ctc_tty_readmodem(info);
  1013. if (again) {
  1014. tasklet_schedule(&info->tasklet);
  1015. }
  1016. }
  1017. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1018. }
  1019. static struct tty_operations ctc_ops = {
  1020. .open = ctc_tty_open,
  1021. .close = ctc_tty_close,
  1022. .write = ctc_tty_write,
  1023. .flush_chars = ctc_tty_flush_chars,
  1024. .write_room = ctc_tty_write_room,
  1025. .chars_in_buffer = ctc_tty_chars_in_buffer,
  1026. .flush_buffer = ctc_tty_flush_buffer,
  1027. .ioctl = ctc_tty_ioctl,
  1028. .throttle = ctc_tty_throttle,
  1029. .unthrottle = ctc_tty_unthrottle,
  1030. .set_termios = ctc_tty_set_termios,
  1031. .hangup = ctc_tty_hangup,
  1032. .tiocmget = ctc_tty_tiocmget,
  1033. .tiocmset = ctc_tty_tiocmset,
  1034. };
  1035. int
  1036. ctc_tty_init(void)
  1037. {
  1038. int i;
  1039. ctc_tty_info *info;
  1040. struct tty_driver *device;
  1041. DBF_TEXT(trace, 2, __FUNCTION__);
  1042. driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
  1043. if (driver == NULL) {
  1044. printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
  1045. return -ENOMEM;
  1046. }
  1047. memset(driver, 0, sizeof(ctc_tty_driver));
  1048. device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
  1049. if (!device) {
  1050. kfree(driver);
  1051. printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
  1052. return -ENOMEM;
  1053. }
  1054. device->devfs_name = "ctc/" CTC_TTY_NAME;
  1055. device->name = CTC_TTY_NAME;
  1056. device->major = CTC_TTY_MAJOR;
  1057. device->minor_start = 0;
  1058. device->type = TTY_DRIVER_TYPE_SERIAL;
  1059. device->subtype = SERIAL_TYPE_NORMAL;
  1060. device->init_termios = tty_std_termios;
  1061. device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1062. device->flags = TTY_DRIVER_REAL_RAW;
  1063. device->driver_name = "ctc_tty",
  1064. tty_set_operations(device, &ctc_ops);
  1065. if (tty_register_driver(device)) {
  1066. printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
  1067. put_tty_driver(device);
  1068. kfree(driver);
  1069. return -1;
  1070. }
  1071. driver->ctc_tty_device = device;
  1072. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
  1073. info = &driver->info[i];
  1074. init_MUTEX(&info->write_sem);
  1075. tasklet_init(&info->tasklet, ctc_tty_task,
  1076. (unsigned long) info);
  1077. info->magic = CTC_ASYNC_MAGIC;
  1078. info->line = i;
  1079. info->tty = 0;
  1080. info->count = 0;
  1081. info->blocked_open = 0;
  1082. init_waitqueue_head(&info->open_wait);
  1083. init_waitqueue_head(&info->close_wait);
  1084. skb_queue_head_init(&info->tx_queue);
  1085. skb_queue_head_init(&info->rx_queue);
  1086. init_timer(&info->stoptimer);
  1087. info->stoptimer.function = ctc_tty_stopdev;
  1088. info->stoptimer.data = (unsigned long)info;
  1089. info->mcr = UART_MCR_RTS;
  1090. }
  1091. return 0;
  1092. }
  1093. int
  1094. ctc_tty_register_netdev(struct net_device *dev) {
  1095. int ttynum;
  1096. char *err;
  1097. char *p;
  1098. DBF_TEXT(trace, 2, __FUNCTION__);
  1099. if ((!dev) || (!dev->name)) {
  1100. printk(KERN_WARNING
  1101. "ctc_tty_register_netdev called "
  1102. "with NULL dev or NULL dev-name\n");
  1103. return -1;
  1104. }
  1105. /*
  1106. * If the name is a format string the caller wants us to
  1107. * do a name allocation : format string must end with %d
  1108. */
  1109. if (strchr(dev->name, '%'))
  1110. {
  1111. int err = dev_alloc_name(dev, dev->name); // dev->name is changed by this
  1112. if (err < 0) {
  1113. printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
  1114. return err;
  1115. }
  1116. }
  1117. for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
  1118. ttynum = simple_strtoul(p, &err, 0);
  1119. if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
  1120. (err && *err)) {
  1121. printk(KERN_WARNING
  1122. "ctc_tty_register_netdev called "
  1123. "with number in name '%s'\n", dev->name);
  1124. return -1;
  1125. }
  1126. if (driver->info[ttynum].netdev) {
  1127. printk(KERN_WARNING
  1128. "ctc_tty_register_netdev called "
  1129. "for already registered device '%s'\n",
  1130. dev->name);
  1131. return -1;
  1132. }
  1133. driver->info[ttynum].netdev = dev;
  1134. return 0;
  1135. }
  1136. void
  1137. ctc_tty_unregister_netdev(struct net_device *dev) {
  1138. int i;
  1139. unsigned long saveflags;
  1140. ctc_tty_info *info = NULL;
  1141. DBF_TEXT(trace, 2, __FUNCTION__);
  1142. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1143. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
  1144. if (driver->info[i].netdev == dev) {
  1145. info = &driver->info[i];
  1146. break;
  1147. }
  1148. if (info) {
  1149. info->netdev = NULL;
  1150. skb_queue_purge(&info->tx_queue);
  1151. skb_queue_purge(&info->rx_queue);
  1152. }
  1153. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1154. }
  1155. void
  1156. ctc_tty_cleanup(void) {
  1157. unsigned long saveflags;
  1158. DBF_TEXT(trace, 2, __FUNCTION__);
  1159. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1160. ctc_tty_shuttingdown = 1;
  1161. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1162. tty_unregister_driver(driver->ctc_tty_device);
  1163. put_tty_driver(driver->ctc_tty_device);
  1164. kfree(driver);
  1165. driver = NULL;
  1166. }