ctctty.c 33 KB

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