ctctty.c 34 KB

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