ctctty.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * $Id: ctctty.c,v 1.26 2004/08/04 11:06:55 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. set_current_state(TASK_INTERRUPTIBLE);
  982. schedule_timeout(HZ/2);
  983. wake_up_interruptible(&info->open_wait);
  984. }
  985. info->flags &= ~(CTC_ASYNC_NORMAL_ACTIVE | CTC_ASYNC_CLOSING);
  986. wake_up_interruptible(&info->close_wait);
  987. spin_unlock_irqrestore(&ctc_tty_lock, flags);
  988. #ifdef CTC_DEBUG_MODEM_OPEN
  989. printk(KERN_DEBUG "ctc_tty_close normal exit\n");
  990. #endif
  991. }
  992. /*
  993. * ctc_tty_hangup() --- called by tty_hangup() when a hangup is signaled.
  994. */
  995. static void
  996. ctc_tty_hangup(struct tty_struct *tty)
  997. {
  998. ctc_tty_info *info = (ctc_tty_info *)tty->driver_data;
  999. unsigned long saveflags;
  1000. DBF_TEXT(trace, 3, __FUNCTION__);
  1001. if (ctc_tty_paranoia_check(info, tty->name, "ctc_tty_hangup"))
  1002. return;
  1003. ctc_tty_shutdown(info);
  1004. info->count = 0;
  1005. info->flags &= ~CTC_ASYNC_NORMAL_ACTIVE;
  1006. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1007. info->tty = 0;
  1008. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1009. wake_up_interruptible(&info->open_wait);
  1010. }
  1011. /*
  1012. * For all online tty's, try sending data to
  1013. * the lower levels.
  1014. */
  1015. static void
  1016. ctc_tty_task(unsigned long arg)
  1017. {
  1018. ctc_tty_info *info = (void *)arg;
  1019. unsigned long saveflags;
  1020. int again;
  1021. DBF_TEXT(trace, 3, __FUNCTION__);
  1022. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1023. if ((!ctc_tty_shuttingdown) && info) {
  1024. again = ctc_tty_tint(info);
  1025. if (!again)
  1026. info->lsr |= UART_LSR_TEMT;
  1027. again |= ctc_tty_readmodem(info);
  1028. if (again) {
  1029. tasklet_schedule(&info->tasklet);
  1030. }
  1031. }
  1032. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1033. }
  1034. static struct tty_operations ctc_ops = {
  1035. .open = ctc_tty_open,
  1036. .close = ctc_tty_close,
  1037. .write = ctc_tty_write,
  1038. .flush_chars = ctc_tty_flush_chars,
  1039. .write_room = ctc_tty_write_room,
  1040. .chars_in_buffer = ctc_tty_chars_in_buffer,
  1041. .flush_buffer = ctc_tty_flush_buffer,
  1042. .ioctl = ctc_tty_ioctl,
  1043. .throttle = ctc_tty_throttle,
  1044. .unthrottle = ctc_tty_unthrottle,
  1045. .set_termios = ctc_tty_set_termios,
  1046. .hangup = ctc_tty_hangup,
  1047. .tiocmget = ctc_tty_tiocmget,
  1048. .tiocmset = ctc_tty_tiocmset,
  1049. };
  1050. int
  1051. ctc_tty_init(void)
  1052. {
  1053. int i;
  1054. ctc_tty_info *info;
  1055. struct tty_driver *device;
  1056. DBF_TEXT(trace, 2, __FUNCTION__);
  1057. driver = kmalloc(sizeof(ctc_tty_driver), GFP_KERNEL);
  1058. if (driver == NULL) {
  1059. printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
  1060. return -ENOMEM;
  1061. }
  1062. memset(driver, 0, sizeof(ctc_tty_driver));
  1063. device = alloc_tty_driver(CTC_TTY_MAX_DEVICES);
  1064. if (!device) {
  1065. kfree(driver);
  1066. printk(KERN_WARNING "Out of memory in ctc_tty_modem_init\n");
  1067. return -ENOMEM;
  1068. }
  1069. device->devfs_name = "ctc/" CTC_TTY_NAME;
  1070. device->name = CTC_TTY_NAME;
  1071. device->major = CTC_TTY_MAJOR;
  1072. device->minor_start = 0;
  1073. device->type = TTY_DRIVER_TYPE_SERIAL;
  1074. device->subtype = SERIAL_TYPE_NORMAL;
  1075. device->init_termios = tty_std_termios;
  1076. device->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1077. device->flags = TTY_DRIVER_REAL_RAW;
  1078. device->driver_name = "ctc_tty",
  1079. tty_set_operations(device, &ctc_ops);
  1080. if (tty_register_driver(device)) {
  1081. printk(KERN_WARNING "ctc_tty: Couldn't register serial-device\n");
  1082. put_tty_driver(device);
  1083. kfree(driver);
  1084. return -1;
  1085. }
  1086. driver->ctc_tty_device = device;
  1087. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++) {
  1088. info = &driver->info[i];
  1089. init_MUTEX(&info->write_sem);
  1090. tasklet_init(&info->tasklet, ctc_tty_task,
  1091. (unsigned long) info);
  1092. info->magic = CTC_ASYNC_MAGIC;
  1093. info->line = i;
  1094. info->tty = 0;
  1095. info->count = 0;
  1096. info->blocked_open = 0;
  1097. init_waitqueue_head(&info->open_wait);
  1098. init_waitqueue_head(&info->close_wait);
  1099. skb_queue_head_init(&info->tx_queue);
  1100. skb_queue_head_init(&info->rx_queue);
  1101. init_timer(&info->stoptimer);
  1102. info->stoptimer.function = ctc_tty_stopdev;
  1103. info->stoptimer.data = (unsigned long)info;
  1104. info->mcr = UART_MCR_RTS;
  1105. }
  1106. return 0;
  1107. }
  1108. int
  1109. ctc_tty_register_netdev(struct net_device *dev) {
  1110. int ttynum;
  1111. char *err;
  1112. char *p;
  1113. DBF_TEXT(trace, 2, __FUNCTION__);
  1114. if ((!dev) || (!dev->name)) {
  1115. printk(KERN_WARNING
  1116. "ctc_tty_register_netdev called "
  1117. "with NULL dev or NULL dev-name\n");
  1118. return -1;
  1119. }
  1120. /*
  1121. * If the name is a format string the caller wants us to
  1122. * do a name allocation : format string must end with %d
  1123. */
  1124. if (strchr(dev->name, '%'))
  1125. {
  1126. int err = dev_alloc_name(dev, dev->name); // dev->name is changed by this
  1127. if (err < 0) {
  1128. printk(KERN_DEBUG "dev_alloc returned error %d\n", err);
  1129. return err;
  1130. }
  1131. }
  1132. for (p = dev->name; p && ((*p < '0') || (*p > '9')); p++);
  1133. ttynum = simple_strtoul(p, &err, 0);
  1134. if ((ttynum < 0) || (ttynum >= CTC_TTY_MAX_DEVICES) ||
  1135. (err && *err)) {
  1136. printk(KERN_WARNING
  1137. "ctc_tty_register_netdev called "
  1138. "with number in name '%s'\n", dev->name);
  1139. return -1;
  1140. }
  1141. if (driver->info[ttynum].netdev) {
  1142. printk(KERN_WARNING
  1143. "ctc_tty_register_netdev called "
  1144. "for already registered device '%s'\n",
  1145. dev->name);
  1146. return -1;
  1147. }
  1148. driver->info[ttynum].netdev = dev;
  1149. return 0;
  1150. }
  1151. void
  1152. ctc_tty_unregister_netdev(struct net_device *dev) {
  1153. int i;
  1154. unsigned long saveflags;
  1155. ctc_tty_info *info = NULL;
  1156. DBF_TEXT(trace, 2, __FUNCTION__);
  1157. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1158. for (i = 0; i < CTC_TTY_MAX_DEVICES; i++)
  1159. if (driver->info[i].netdev == dev) {
  1160. info = &driver->info[i];
  1161. break;
  1162. }
  1163. if (info) {
  1164. info->netdev = NULL;
  1165. skb_queue_purge(&info->tx_queue);
  1166. skb_queue_purge(&info->rx_queue);
  1167. }
  1168. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1169. }
  1170. void
  1171. ctc_tty_cleanup(void) {
  1172. unsigned long saveflags;
  1173. DBF_TEXT(trace, 2, __FUNCTION__);
  1174. spin_lock_irqsave(&ctc_tty_lock, saveflags);
  1175. ctc_tty_shuttingdown = 1;
  1176. spin_unlock_irqrestore(&ctc_tty_lock, saveflags);
  1177. tty_unregister_driver(driver->ctc_tty_device);
  1178. put_tty_driver(driver->ctc_tty_device);
  1179. kfree(driver);
  1180. driver = NULL;
  1181. }