amiserial.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /*
  2. * linux/drivers/char/amiserial.c
  3. *
  4. * Serial driver for the amiga builtin port.
  5. *
  6. * This code was created by taking serial.c version 4.30 from kernel
  7. * release 2.3.22, replacing all hardware related stuff with the
  8. * corresponding amiga hardware actions, and removing all irrelevant
  9. * code. As a consequence, it uses many of the constants and names
  10. * associated with the registers and bits of 16550 compatible UARTS -
  11. * but only to keep track of status, etc in the state variables. It
  12. * was done this was to make it easier to keep the code in line with
  13. * (non hardware specific) changes to serial.c.
  14. *
  15. * The port is registered with the tty driver as minor device 64, and
  16. * therefore other ports should should only use 65 upwards.
  17. *
  18. * Richard Lucock 28/12/99
  19. *
  20. * Copyright (C) 1991, 1992 Linus Torvalds
  21. * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
  22. * 1998, 1999 Theodore Ts'o
  23. *
  24. */
  25. /*
  26. * Serial driver configuration section. Here are the various options:
  27. *
  28. * SERIAL_PARANOIA_CHECK
  29. * Check the magic number for the async_structure where
  30. * ever possible.
  31. */
  32. #include <linux/config.h>
  33. #include <linux/delay.h>
  34. #undef SERIAL_PARANOIA_CHECK
  35. #define SERIAL_DO_RESTART
  36. /* Set of debugging defines */
  37. #undef SERIAL_DEBUG_INTR
  38. #undef SERIAL_DEBUG_OPEN
  39. #undef SERIAL_DEBUG_FLOW
  40. #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  41. /* Sanity checks */
  42. #define SERIAL_INLINE
  43. #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
  44. #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
  45. tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
  46. #else
  47. #define DBG_CNT(s)
  48. #endif
  49. /*
  50. * End of serial driver configuration section.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/types.h>
  54. #include <linux/serial.h>
  55. #include <linux/serialP.h>
  56. #include <linux/serial_reg.h>
  57. static char *serial_version = "4.30";
  58. #include <linux/errno.h>
  59. #include <linux/signal.h>
  60. #include <linux/sched.h>
  61. #include <linux/kernel.h>
  62. #include <linux/timer.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/tty.h>
  65. #include <linux/tty_flip.h>
  66. #include <linux/console.h>
  67. #include <linux/major.h>
  68. #include <linux/string.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/ptrace.h>
  71. #include <linux/ioport.h>
  72. #include <linux/mm.h>
  73. #include <linux/slab.h>
  74. #include <linux/init.h>
  75. #include <linux/bitops.h>
  76. #include <asm/setup.h>
  77. #include <asm/system.h>
  78. #include <asm/irq.h>
  79. #include <asm/amigahw.h>
  80. #include <asm/amigaints.h>
  81. #ifdef SERIAL_INLINE
  82. #define _INLINE_ inline
  83. #endif
  84. static char *serial_name = "Amiga-builtin serial driver";
  85. static struct tty_driver *serial_driver;
  86. /* number of characters left in xmit buffer before we ask for more */
  87. #define WAKEUP_CHARS 256
  88. static struct async_struct *IRQ_ports;
  89. static unsigned char current_ctl_bits;
  90. static void change_speed(struct async_struct *info, struct termios *old);
  91. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  92. static struct serial_state rs_table[1];
  93. #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
  94. /*
  95. * tmp_buf is used as a temporary buffer by serial_write. We need to
  96. * lock it in case the copy_from_user blocks while swapping in a page,
  97. * and some other program tries to do a serial write at the same time.
  98. * Since the lock will only come under contention when the system is
  99. * swapping and available memory is low, it makes sense to share one
  100. * buffer across all the serial ports, since it significantly saves
  101. * memory if large numbers of serial ports are open.
  102. */
  103. static unsigned char *tmp_buf;
  104. static DECLARE_MUTEX(tmp_buf_sem);
  105. #include <asm/uaccess.h>
  106. #define serial_isroot() (capable(CAP_SYS_ADMIN))
  107. static inline int serial_paranoia_check(struct async_struct *info,
  108. char *name, const char *routine)
  109. {
  110. #ifdef SERIAL_PARANOIA_CHECK
  111. static const char *badmagic =
  112. "Warning: bad magic number for serial struct (%s) in %s\n";
  113. static const char *badinfo =
  114. "Warning: null async_struct for (%s) in %s\n";
  115. if (!info) {
  116. printk(badinfo, name, routine);
  117. return 1;
  118. }
  119. if (info->magic != SERIAL_MAGIC) {
  120. printk(badmagic, name, routine);
  121. return 1;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. /* some serial hardware definitions */
  127. #define SDR_OVRUN (1<<15)
  128. #define SDR_RBF (1<<14)
  129. #define SDR_TBE (1<<13)
  130. #define SDR_TSRE (1<<12)
  131. #define SERPER_PARENB (1<<15)
  132. #define AC_SETCLR (1<<15)
  133. #define AC_UARTBRK (1<<11)
  134. #define SER_DTR (1<<7)
  135. #define SER_RTS (1<<6)
  136. #define SER_DCD (1<<5)
  137. #define SER_CTS (1<<4)
  138. #define SER_DSR (1<<3)
  139. static __inline__ void rtsdtr_ctrl(int bits)
  140. {
  141. ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
  142. }
  143. /*
  144. * ------------------------------------------------------------
  145. * rs_stop() and rs_start()
  146. *
  147. * This routines are called before setting or resetting tty->stopped.
  148. * They enable or disable transmitter interrupts, as necessary.
  149. * ------------------------------------------------------------
  150. */
  151. static void rs_stop(struct tty_struct *tty)
  152. {
  153. struct async_struct *info = (struct async_struct *)tty->driver_data;
  154. unsigned long flags;
  155. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  156. return;
  157. local_irq_save(flags);
  158. if (info->IER & UART_IER_THRI) {
  159. info->IER &= ~UART_IER_THRI;
  160. /* disable Tx interrupt and remove any pending interrupts */
  161. custom.intena = IF_TBE;
  162. mb();
  163. custom.intreq = IF_TBE;
  164. mb();
  165. }
  166. local_irq_restore(flags);
  167. }
  168. static void rs_start(struct tty_struct *tty)
  169. {
  170. struct async_struct *info = (struct async_struct *)tty->driver_data;
  171. unsigned long flags;
  172. if (serial_paranoia_check(info, tty->name, "rs_start"))
  173. return;
  174. local_irq_save(flags);
  175. if (info->xmit.head != info->xmit.tail
  176. && info->xmit.buf
  177. && !(info->IER & UART_IER_THRI)) {
  178. info->IER |= UART_IER_THRI;
  179. custom.intena = IF_SETCLR | IF_TBE;
  180. mb();
  181. /* set a pending Tx Interrupt, transmitter should restart now */
  182. custom.intreq = IF_SETCLR | IF_TBE;
  183. mb();
  184. }
  185. local_irq_restore(flags);
  186. }
  187. /*
  188. * ----------------------------------------------------------------------
  189. *
  190. * Here starts the interrupt handling routines. All of the following
  191. * subroutines are declared as inline and are folded into
  192. * rs_interrupt(). They were separated out for readability's sake.
  193. *
  194. * Note: rs_interrupt() is a "fast" interrupt, which means that it
  195. * runs with interrupts turned off. People who may want to modify
  196. * rs_interrupt() should try to keep the interrupt handler as fast as
  197. * possible. After you are done making modifications, it is not a bad
  198. * idea to do:
  199. *
  200. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  201. *
  202. * and look at the resulting assemble code in serial.s.
  203. *
  204. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  205. * -----------------------------------------------------------------------
  206. */
  207. /*
  208. * This routine is used by the interrupt handler to schedule
  209. * processing in the software interrupt portion of the driver.
  210. */
  211. static _INLINE_ void rs_sched_event(struct async_struct *info,
  212. int event)
  213. {
  214. info->event |= 1 << event;
  215. tasklet_schedule(&info->tlet);
  216. }
  217. static _INLINE_ void receive_chars(struct async_struct *info)
  218. {
  219. int status;
  220. int serdatr;
  221. struct tty_struct *tty = info->tty;
  222. unsigned char ch;
  223. struct async_icount *icount;
  224. icount = &info->state->icount;
  225. status = UART_LSR_DR; /* We obviously have a character! */
  226. serdatr = custom.serdatr;
  227. mb();
  228. custom.intreq = IF_RBF;
  229. mb();
  230. if((serdatr & 0x1ff) == 0)
  231. status |= UART_LSR_BI;
  232. if(serdatr & SDR_OVRUN)
  233. status |= UART_LSR_OE;
  234. ch = serdatr & 0xff;
  235. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  236. goto ignore_char;
  237. *tty->flip.char_buf_ptr = ch;
  238. icount->rx++;
  239. #ifdef SERIAL_DEBUG_INTR
  240. printk("DR%02x:%02x...", ch, status);
  241. #endif
  242. *tty->flip.flag_buf_ptr = 0;
  243. /*
  244. * We don't handle parity or frame errors - but I have left
  245. * the code in, since I'm not sure that the errors can't be
  246. * detected.
  247. */
  248. if (status & (UART_LSR_BI | UART_LSR_PE |
  249. UART_LSR_FE | UART_LSR_OE)) {
  250. /*
  251. * For statistics only
  252. */
  253. if (status & UART_LSR_BI) {
  254. status &= ~(UART_LSR_FE | UART_LSR_PE);
  255. icount->brk++;
  256. } else if (status & UART_LSR_PE)
  257. icount->parity++;
  258. else if (status & UART_LSR_FE)
  259. icount->frame++;
  260. if (status & UART_LSR_OE)
  261. icount->overrun++;
  262. /*
  263. * Now check to see if character should be
  264. * ignored, and mask off conditions which
  265. * should be ignored.
  266. */
  267. if (status & info->ignore_status_mask)
  268. goto ignore_char;
  269. status &= info->read_status_mask;
  270. if (status & (UART_LSR_BI)) {
  271. #ifdef SERIAL_DEBUG_INTR
  272. printk("handling break....");
  273. #endif
  274. *tty->flip.flag_buf_ptr = TTY_BREAK;
  275. if (info->flags & ASYNC_SAK)
  276. do_SAK(tty);
  277. } else if (status & UART_LSR_PE)
  278. *tty->flip.flag_buf_ptr = TTY_PARITY;
  279. else if (status & UART_LSR_FE)
  280. *tty->flip.flag_buf_ptr = TTY_FRAME;
  281. if (status & UART_LSR_OE) {
  282. /*
  283. * Overrun is special, since it's
  284. * reported immediately, and doesn't
  285. * affect the current character
  286. */
  287. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  288. tty->flip.count++;
  289. tty->flip.flag_buf_ptr++;
  290. tty->flip.char_buf_ptr++;
  291. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  292. }
  293. }
  294. }
  295. tty->flip.flag_buf_ptr++;
  296. tty->flip.char_buf_ptr++;
  297. tty->flip.count++;
  298. ignore_char:
  299. tty_flip_buffer_push(tty);
  300. }
  301. static _INLINE_ void transmit_chars(struct async_struct *info)
  302. {
  303. custom.intreq = IF_TBE;
  304. mb();
  305. if (info->x_char) {
  306. custom.serdat = info->x_char | 0x100;
  307. mb();
  308. info->state->icount.tx++;
  309. info->x_char = 0;
  310. return;
  311. }
  312. if (info->xmit.head == info->xmit.tail
  313. || info->tty->stopped
  314. || info->tty->hw_stopped) {
  315. info->IER &= ~UART_IER_THRI;
  316. custom.intena = IF_TBE;
  317. mb();
  318. return;
  319. }
  320. custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
  321. mb();
  322. info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
  323. info->state->icount.tx++;
  324. if (CIRC_CNT(info->xmit.head,
  325. info->xmit.tail,
  326. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  327. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  328. #ifdef SERIAL_DEBUG_INTR
  329. printk("THRE...");
  330. #endif
  331. if (info->xmit.head == info->xmit.tail) {
  332. custom.intena = IF_TBE;
  333. mb();
  334. info->IER &= ~UART_IER_THRI;
  335. }
  336. }
  337. static _INLINE_ void check_modem_status(struct async_struct *info)
  338. {
  339. unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  340. unsigned char dstatus;
  341. struct async_icount *icount;
  342. /* Determine bits that have changed */
  343. dstatus = status ^ current_ctl_bits;
  344. current_ctl_bits = status;
  345. if (dstatus) {
  346. icount = &info->state->icount;
  347. /* update input line counters */
  348. if (dstatus & SER_DSR)
  349. icount->dsr++;
  350. if (dstatus & SER_DCD) {
  351. icount->dcd++;
  352. #ifdef CONFIG_HARD_PPS
  353. if ((info->flags & ASYNC_HARDPPS_CD) &&
  354. !(status & SER_DCD))
  355. hardpps();
  356. #endif
  357. }
  358. if (dstatus & SER_CTS)
  359. icount->cts++;
  360. wake_up_interruptible(&info->delta_msr_wait);
  361. }
  362. if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
  363. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  364. printk("ttyS%d CD now %s...", info->line,
  365. (!(status & SER_DCD)) ? "on" : "off");
  366. #endif
  367. if (!(status & SER_DCD))
  368. wake_up_interruptible(&info->open_wait);
  369. else {
  370. #ifdef SERIAL_DEBUG_OPEN
  371. printk("doing serial hangup...");
  372. #endif
  373. if (info->tty)
  374. tty_hangup(info->tty);
  375. }
  376. }
  377. if (info->flags & ASYNC_CTS_FLOW) {
  378. if (info->tty->hw_stopped) {
  379. if (!(status & SER_CTS)) {
  380. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  381. printk("CTS tx start...");
  382. #endif
  383. info->tty->hw_stopped = 0;
  384. info->IER |= UART_IER_THRI;
  385. custom.intena = IF_SETCLR | IF_TBE;
  386. mb();
  387. /* set a pending Tx Interrupt, transmitter should restart now */
  388. custom.intreq = IF_SETCLR | IF_TBE;
  389. mb();
  390. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  391. return;
  392. }
  393. } else {
  394. if ((status & SER_CTS)) {
  395. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  396. printk("CTS tx stop...");
  397. #endif
  398. info->tty->hw_stopped = 1;
  399. info->IER &= ~UART_IER_THRI;
  400. /* disable Tx interrupt and remove any pending interrupts */
  401. custom.intena = IF_TBE;
  402. mb();
  403. custom.intreq = IF_TBE;
  404. mb();
  405. }
  406. }
  407. }
  408. }
  409. static irqreturn_t ser_vbl_int( int irq, void *data, struct pt_regs *regs)
  410. {
  411. /* vbl is just a periodic interrupt we tie into to update modem status */
  412. struct async_struct * info = IRQ_ports;
  413. /*
  414. * TBD - is it better to unregister from this interrupt or to
  415. * ignore it if MSI is clear ?
  416. */
  417. if(info->IER & UART_IER_MSI)
  418. check_modem_status(info);
  419. return IRQ_HANDLED;
  420. }
  421. static irqreturn_t ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
  422. {
  423. struct async_struct * info;
  424. #ifdef SERIAL_DEBUG_INTR
  425. printk("ser_rx_int...");
  426. #endif
  427. info = IRQ_ports;
  428. if (!info || !info->tty)
  429. return IRQ_NONE;
  430. receive_chars(info);
  431. info->last_active = jiffies;
  432. #ifdef SERIAL_DEBUG_INTR
  433. printk("end.\n");
  434. #endif
  435. return IRQ_HANDLED;
  436. }
  437. static irqreturn_t ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
  438. {
  439. struct async_struct * info;
  440. if (custom.serdatr & SDR_TBE) {
  441. #ifdef SERIAL_DEBUG_INTR
  442. printk("ser_tx_int...");
  443. #endif
  444. info = IRQ_ports;
  445. if (!info || !info->tty)
  446. return IRQ_NONE;
  447. transmit_chars(info);
  448. info->last_active = jiffies;
  449. #ifdef SERIAL_DEBUG_INTR
  450. printk("end.\n");
  451. #endif
  452. }
  453. return IRQ_HANDLED;
  454. }
  455. /*
  456. * -------------------------------------------------------------------
  457. * Here ends the serial interrupt routines.
  458. * -------------------------------------------------------------------
  459. */
  460. /*
  461. * This routine is used to handle the "bottom half" processing for the
  462. * serial driver, known also the "software interrupt" processing.
  463. * This processing is done at the kernel interrupt level, after the
  464. * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
  465. * is where time-consuming activities which can not be done in the
  466. * interrupt driver proper are done; the interrupt driver schedules
  467. * them using rs_sched_event(), and they get done here.
  468. */
  469. static void do_softint(unsigned long private_)
  470. {
  471. struct async_struct *info = (struct async_struct *) private_;
  472. struct tty_struct *tty;
  473. tty = info->tty;
  474. if (!tty)
  475. return;
  476. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  477. tty_wakeup(tty);
  478. wake_up_interruptible(&tty->write_wait);
  479. }
  480. }
  481. /*
  482. * ---------------------------------------------------------------
  483. * Low level utility subroutines for the serial driver: routines to
  484. * figure out the appropriate timeout for an interrupt chain, routines
  485. * to initialize and startup a serial port, and routines to shutdown a
  486. * serial port. Useful stuff like that.
  487. * ---------------------------------------------------------------
  488. */
  489. static int startup(struct async_struct * info)
  490. {
  491. unsigned long flags;
  492. int retval=0;
  493. unsigned long page;
  494. page = get_zeroed_page(GFP_KERNEL);
  495. if (!page)
  496. return -ENOMEM;
  497. local_irq_save(flags);
  498. if (info->flags & ASYNC_INITIALIZED) {
  499. free_page(page);
  500. goto errout;
  501. }
  502. if (info->xmit.buf)
  503. free_page(page);
  504. else
  505. info->xmit.buf = (unsigned char *) page;
  506. #ifdef SERIAL_DEBUG_OPEN
  507. printk("starting up ttys%d ...", info->line);
  508. #endif
  509. /* Clear anything in the input buffer */
  510. custom.intreq = IF_RBF;
  511. mb();
  512. retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
  513. if (retval) {
  514. if (serial_isroot()) {
  515. if (info->tty)
  516. set_bit(TTY_IO_ERROR,
  517. &info->tty->flags);
  518. retval = 0;
  519. }
  520. goto errout;
  521. }
  522. /* enable both Rx and Tx interrupts */
  523. custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
  524. mb();
  525. info->IER = UART_IER_MSI;
  526. /* remember current state of the DCD and CTS bits */
  527. current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  528. IRQ_ports = info;
  529. info->MCR = 0;
  530. if (info->tty->termios->c_cflag & CBAUD)
  531. info->MCR = SER_DTR | SER_RTS;
  532. rtsdtr_ctrl(info->MCR);
  533. if (info->tty)
  534. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  535. info->xmit.head = info->xmit.tail = 0;
  536. /*
  537. * Set up the tty->alt_speed kludge
  538. */
  539. if (info->tty) {
  540. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  541. info->tty->alt_speed = 57600;
  542. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  543. info->tty->alt_speed = 115200;
  544. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  545. info->tty->alt_speed = 230400;
  546. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  547. info->tty->alt_speed = 460800;
  548. }
  549. /*
  550. * and set the speed of the serial port
  551. */
  552. change_speed(info, NULL);
  553. info->flags |= ASYNC_INITIALIZED;
  554. local_irq_restore(flags);
  555. return 0;
  556. errout:
  557. local_irq_restore(flags);
  558. return retval;
  559. }
  560. /*
  561. * This routine will shutdown a serial port; interrupts are disabled, and
  562. * DTR is dropped if the hangup on close termio flag is on.
  563. */
  564. static void shutdown(struct async_struct * info)
  565. {
  566. unsigned long flags;
  567. struct serial_state *state;
  568. if (!(info->flags & ASYNC_INITIALIZED))
  569. return;
  570. state = info->state;
  571. #ifdef SERIAL_DEBUG_OPEN
  572. printk("Shutting down serial port %d ....\n", info->line);
  573. #endif
  574. local_irq_save(flags); /* Disable interrupts */
  575. /*
  576. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  577. * here so the queue might never be waken up
  578. */
  579. wake_up_interruptible(&info->delta_msr_wait);
  580. IRQ_ports = NULL;
  581. /*
  582. * Free the IRQ, if necessary
  583. */
  584. free_irq(IRQ_AMIGA_VERTB, info);
  585. if (info->xmit.buf) {
  586. free_page((unsigned long) info->xmit.buf);
  587. info->xmit.buf = NULL;
  588. }
  589. info->IER = 0;
  590. custom.intena = IF_RBF | IF_TBE;
  591. mb();
  592. /* disable break condition */
  593. custom.adkcon = AC_UARTBRK;
  594. mb();
  595. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  596. info->MCR &= ~(SER_DTR|SER_RTS);
  597. rtsdtr_ctrl(info->MCR);
  598. if (info->tty)
  599. set_bit(TTY_IO_ERROR, &info->tty->flags);
  600. info->flags &= ~ASYNC_INITIALIZED;
  601. local_irq_restore(flags);
  602. }
  603. /*
  604. * This routine is called to set the UART divisor registers to match
  605. * the specified baud rate for a serial port.
  606. */
  607. static void change_speed(struct async_struct *info,
  608. struct termios *old_termios)
  609. {
  610. int quot = 0, baud_base, baud;
  611. unsigned cflag, cval = 0;
  612. int bits;
  613. unsigned long flags;
  614. if (!info->tty || !info->tty->termios)
  615. return;
  616. cflag = info->tty->termios->c_cflag;
  617. /* Byte size is always 8 bits plus parity bit if requested */
  618. cval = 3; bits = 10;
  619. if (cflag & CSTOPB) {
  620. cval |= 0x04;
  621. bits++;
  622. }
  623. if (cflag & PARENB) {
  624. cval |= UART_LCR_PARITY;
  625. bits++;
  626. }
  627. if (!(cflag & PARODD))
  628. cval |= UART_LCR_EPAR;
  629. #ifdef CMSPAR
  630. if (cflag & CMSPAR)
  631. cval |= UART_LCR_SPAR;
  632. #endif
  633. /* Determine divisor based on baud rate */
  634. baud = tty_get_baud_rate(info->tty);
  635. if (!baud)
  636. baud = 9600; /* B0 transition handled in rs_set_termios */
  637. baud_base = info->state->baud_base;
  638. if (baud == 38400 &&
  639. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  640. quot = info->state->custom_divisor;
  641. else {
  642. if (baud == 134)
  643. /* Special case since 134 is really 134.5 */
  644. quot = (2*baud_base / 269);
  645. else if (baud)
  646. quot = baud_base / baud;
  647. }
  648. /* If the quotient is zero refuse the change */
  649. if (!quot && old_termios) {
  650. info->tty->termios->c_cflag &= ~CBAUD;
  651. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  652. baud = tty_get_baud_rate(info->tty);
  653. if (!baud)
  654. baud = 9600;
  655. if (baud == 38400 &&
  656. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  657. quot = info->state->custom_divisor;
  658. else {
  659. if (baud == 134)
  660. /* Special case since 134 is really 134.5 */
  661. quot = (2*baud_base / 269);
  662. else if (baud)
  663. quot = baud_base / baud;
  664. }
  665. }
  666. /* As a last resort, if the quotient is zero, default to 9600 bps */
  667. if (!quot)
  668. quot = baud_base / 9600;
  669. info->quot = quot;
  670. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  671. info->timeout += HZ/50; /* Add .02 seconds of slop */
  672. /* CTS flow control flag and modem status interrupts */
  673. info->IER &= ~UART_IER_MSI;
  674. if (info->flags & ASYNC_HARDPPS_CD)
  675. info->IER |= UART_IER_MSI;
  676. if (cflag & CRTSCTS) {
  677. info->flags |= ASYNC_CTS_FLOW;
  678. info->IER |= UART_IER_MSI;
  679. } else
  680. info->flags &= ~ASYNC_CTS_FLOW;
  681. if (cflag & CLOCAL)
  682. info->flags &= ~ASYNC_CHECK_CD;
  683. else {
  684. info->flags |= ASYNC_CHECK_CD;
  685. info->IER |= UART_IER_MSI;
  686. }
  687. /* TBD:
  688. * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
  689. */
  690. /*
  691. * Set up parity check flag
  692. */
  693. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  694. info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  695. if (I_INPCK(info->tty))
  696. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  697. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  698. info->read_status_mask |= UART_LSR_BI;
  699. /*
  700. * Characters to ignore
  701. */
  702. info->ignore_status_mask = 0;
  703. if (I_IGNPAR(info->tty))
  704. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  705. if (I_IGNBRK(info->tty)) {
  706. info->ignore_status_mask |= UART_LSR_BI;
  707. /*
  708. * If we're ignore parity and break indicators, ignore
  709. * overruns too. (For real raw support).
  710. */
  711. if (I_IGNPAR(info->tty))
  712. info->ignore_status_mask |= UART_LSR_OE;
  713. }
  714. /*
  715. * !!! ignore all characters if CREAD is not set
  716. */
  717. if ((cflag & CREAD) == 0)
  718. info->ignore_status_mask |= UART_LSR_DR;
  719. local_irq_save(flags);
  720. {
  721. short serper;
  722. /* Set up the baud rate */
  723. serper = quot - 1;
  724. /* Enable or disable parity bit */
  725. if(cval & UART_LCR_PARITY)
  726. serper |= (SERPER_PARENB);
  727. custom.serper = serper;
  728. mb();
  729. }
  730. info->LCR = cval; /* Save LCR */
  731. local_irq_restore(flags);
  732. }
  733. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  734. {
  735. struct async_struct *info;
  736. unsigned long flags;
  737. if (!tty)
  738. return;
  739. info = tty->driver_data;
  740. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  741. return;
  742. if (!info->xmit.buf)
  743. return;
  744. local_irq_save(flags);
  745. if (CIRC_SPACE(info->xmit.head,
  746. info->xmit.tail,
  747. SERIAL_XMIT_SIZE) == 0) {
  748. local_irq_restore(flags);
  749. return;
  750. }
  751. info->xmit.buf[info->xmit.head++] = ch;
  752. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  753. local_irq_restore(flags);
  754. }
  755. static void rs_flush_chars(struct tty_struct *tty)
  756. {
  757. struct async_struct *info = (struct async_struct *)tty->driver_data;
  758. unsigned long flags;
  759. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  760. return;
  761. if (info->xmit.head == info->xmit.tail
  762. || tty->stopped
  763. || tty->hw_stopped
  764. || !info->xmit.buf)
  765. return;
  766. local_irq_save(flags);
  767. info->IER |= UART_IER_THRI;
  768. custom.intena = IF_SETCLR | IF_TBE;
  769. mb();
  770. /* set a pending Tx Interrupt, transmitter should restart now */
  771. custom.intreq = IF_SETCLR | IF_TBE;
  772. mb();
  773. local_irq_restore(flags);
  774. }
  775. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  776. {
  777. int c, ret = 0;
  778. struct async_struct *info;
  779. unsigned long flags;
  780. if (!tty)
  781. return 0;
  782. info = tty->driver_data;
  783. if (serial_paranoia_check(info, tty->name, "rs_write"))
  784. return 0;
  785. if (!info->xmit.buf || !tmp_buf)
  786. return 0;
  787. local_save_flags(flags);
  788. local_irq_disable();
  789. while (1) {
  790. c = CIRC_SPACE_TO_END(info->xmit.head,
  791. info->xmit.tail,
  792. SERIAL_XMIT_SIZE);
  793. if (count < c)
  794. c = count;
  795. if (c <= 0) {
  796. break;
  797. }
  798. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  799. info->xmit.head = ((info->xmit.head + c) &
  800. (SERIAL_XMIT_SIZE-1));
  801. buf += c;
  802. count -= c;
  803. ret += c;
  804. }
  805. local_irq_restore(flags);
  806. if (info->xmit.head != info->xmit.tail
  807. && !tty->stopped
  808. && !tty->hw_stopped
  809. && !(info->IER & UART_IER_THRI)) {
  810. info->IER |= UART_IER_THRI;
  811. local_irq_disable();
  812. custom.intena = IF_SETCLR | IF_TBE;
  813. mb();
  814. /* set a pending Tx Interrupt, transmitter should restart now */
  815. custom.intreq = IF_SETCLR | IF_TBE;
  816. mb();
  817. local_irq_restore(flags);
  818. }
  819. return ret;
  820. }
  821. static int rs_write_room(struct tty_struct *tty)
  822. {
  823. struct async_struct *info = (struct async_struct *)tty->driver_data;
  824. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  825. return 0;
  826. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  827. }
  828. static int rs_chars_in_buffer(struct tty_struct *tty)
  829. {
  830. struct async_struct *info = (struct async_struct *)tty->driver_data;
  831. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  832. return 0;
  833. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  834. }
  835. static void rs_flush_buffer(struct tty_struct *tty)
  836. {
  837. struct async_struct *info = (struct async_struct *)tty->driver_data;
  838. unsigned long flags;
  839. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  840. return;
  841. local_irq_save(flags);
  842. info->xmit.head = info->xmit.tail = 0;
  843. local_irq_restore(flags);
  844. wake_up_interruptible(&tty->write_wait);
  845. tty_wakeup(tty);
  846. }
  847. /*
  848. * This function is used to send a high-priority XON/XOFF character to
  849. * the device
  850. */
  851. static void rs_send_xchar(struct tty_struct *tty, char ch)
  852. {
  853. struct async_struct *info = (struct async_struct *)tty->driver_data;
  854. unsigned long flags;
  855. if (serial_paranoia_check(info, tty->name, "rs_send_char"))
  856. return;
  857. info->x_char = ch;
  858. if (ch) {
  859. /* Make sure transmit interrupts are on */
  860. /* Check this ! */
  861. local_irq_save(flags);
  862. if(!(custom.intenar & IF_TBE)) {
  863. custom.intena = IF_SETCLR | IF_TBE;
  864. mb();
  865. /* set a pending Tx Interrupt, transmitter should restart now */
  866. custom.intreq = IF_SETCLR | IF_TBE;
  867. mb();
  868. }
  869. local_irq_restore(flags);
  870. info->IER |= UART_IER_THRI;
  871. }
  872. }
  873. /*
  874. * ------------------------------------------------------------
  875. * rs_throttle()
  876. *
  877. * This routine is called by the upper-layer tty layer to signal that
  878. * incoming characters should be throttled.
  879. * ------------------------------------------------------------
  880. */
  881. static void rs_throttle(struct tty_struct * tty)
  882. {
  883. struct async_struct *info = (struct async_struct *)tty->driver_data;
  884. unsigned long flags;
  885. #ifdef SERIAL_DEBUG_THROTTLE
  886. char buf[64];
  887. printk("throttle %s: %d....\n", tty_name(tty, buf),
  888. tty->ldisc.chars_in_buffer(tty));
  889. #endif
  890. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  891. return;
  892. if (I_IXOFF(tty))
  893. rs_send_xchar(tty, STOP_CHAR(tty));
  894. if (tty->termios->c_cflag & CRTSCTS)
  895. info->MCR &= ~SER_RTS;
  896. local_irq_save(flags);
  897. rtsdtr_ctrl(info->MCR);
  898. local_irq_restore(flags);
  899. }
  900. static void rs_unthrottle(struct tty_struct * tty)
  901. {
  902. struct async_struct *info = (struct async_struct *)tty->driver_data;
  903. unsigned long flags;
  904. #ifdef SERIAL_DEBUG_THROTTLE
  905. char buf[64];
  906. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  907. tty->ldisc.chars_in_buffer(tty));
  908. #endif
  909. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  910. return;
  911. if (I_IXOFF(tty)) {
  912. if (info->x_char)
  913. info->x_char = 0;
  914. else
  915. rs_send_xchar(tty, START_CHAR(tty));
  916. }
  917. if (tty->termios->c_cflag & CRTSCTS)
  918. info->MCR |= SER_RTS;
  919. local_irq_save(flags);
  920. rtsdtr_ctrl(info->MCR);
  921. local_irq_restore(flags);
  922. }
  923. /*
  924. * ------------------------------------------------------------
  925. * rs_ioctl() and friends
  926. * ------------------------------------------------------------
  927. */
  928. static int get_serial_info(struct async_struct * info,
  929. struct serial_struct * retinfo)
  930. {
  931. struct serial_struct tmp;
  932. struct serial_state *state = info->state;
  933. if (!retinfo)
  934. return -EFAULT;
  935. memset(&tmp, 0, sizeof(tmp));
  936. tmp.type = state->type;
  937. tmp.line = state->line;
  938. tmp.port = state->port;
  939. tmp.irq = state->irq;
  940. tmp.flags = state->flags;
  941. tmp.xmit_fifo_size = state->xmit_fifo_size;
  942. tmp.baud_base = state->baud_base;
  943. tmp.close_delay = state->close_delay;
  944. tmp.closing_wait = state->closing_wait;
  945. tmp.custom_divisor = state->custom_divisor;
  946. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  947. return -EFAULT;
  948. return 0;
  949. }
  950. static int set_serial_info(struct async_struct * info,
  951. struct serial_struct * new_info)
  952. {
  953. struct serial_struct new_serial;
  954. struct serial_state old_state, *state;
  955. unsigned int change_irq,change_port;
  956. int retval = 0;
  957. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  958. return -EFAULT;
  959. state = info->state;
  960. old_state = *state;
  961. change_irq = new_serial.irq != state->irq;
  962. change_port = (new_serial.port != state->port);
  963. if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
  964. return -EINVAL;
  965. if (!serial_isroot()) {
  966. if ((new_serial.baud_base != state->baud_base) ||
  967. (new_serial.close_delay != state->close_delay) ||
  968. (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
  969. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  970. (state->flags & ~ASYNC_USR_MASK)))
  971. return -EPERM;
  972. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  973. (new_serial.flags & ASYNC_USR_MASK));
  974. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  975. (new_serial.flags & ASYNC_USR_MASK));
  976. state->custom_divisor = new_serial.custom_divisor;
  977. goto check_and_exit;
  978. }
  979. if (new_serial.baud_base < 9600)
  980. return -EINVAL;
  981. /*
  982. * OK, past this point, all the error checking has been done.
  983. * At this point, we start making changes.....
  984. */
  985. state->baud_base = new_serial.baud_base;
  986. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  987. (new_serial.flags & ASYNC_FLAGS));
  988. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  989. (info->flags & ASYNC_INTERNAL_FLAGS));
  990. state->custom_divisor = new_serial.custom_divisor;
  991. state->close_delay = new_serial.close_delay * HZ/100;
  992. state->closing_wait = new_serial.closing_wait * HZ/100;
  993. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  994. check_and_exit:
  995. if (info->flags & ASYNC_INITIALIZED) {
  996. if (((old_state.flags & ASYNC_SPD_MASK) !=
  997. (state->flags & ASYNC_SPD_MASK)) ||
  998. (old_state.custom_divisor != state->custom_divisor)) {
  999. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  1000. info->tty->alt_speed = 57600;
  1001. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  1002. info->tty->alt_speed = 115200;
  1003. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  1004. info->tty->alt_speed = 230400;
  1005. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  1006. info->tty->alt_speed = 460800;
  1007. change_speed(info, NULL);
  1008. }
  1009. } else
  1010. retval = startup(info);
  1011. return retval;
  1012. }
  1013. /*
  1014. * get_lsr_info - get line status register info
  1015. *
  1016. * Purpose: Let user call ioctl() to get info when the UART physically
  1017. * is emptied. On bus types like RS485, the transmitter must
  1018. * release the bus after transmitting. This must be done when
  1019. * the transmit shift register is empty, not be done when the
  1020. * transmit holding register is empty. This functionality
  1021. * allows an RS485 driver to be written in user space.
  1022. */
  1023. static int get_lsr_info(struct async_struct * info, unsigned int *value)
  1024. {
  1025. unsigned char status;
  1026. unsigned int result;
  1027. unsigned long flags;
  1028. local_irq_save(flags);
  1029. status = custom.serdatr;
  1030. mb();
  1031. local_irq_restore(flags);
  1032. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  1033. if (copy_to_user(value, &result, sizeof(int)))
  1034. return -EFAULT;
  1035. return 0;
  1036. }
  1037. static int rs_tiocmget(struct tty_struct *tty, struct file *file)
  1038. {
  1039. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1040. unsigned char control, status;
  1041. unsigned long flags;
  1042. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1043. return -ENODEV;
  1044. if (tty->flags & (1 << TTY_IO_ERROR))
  1045. return -EIO;
  1046. control = info->MCR;
  1047. local_irq_save(flags);
  1048. status = ciab.pra;
  1049. local_irq_restore(flags);
  1050. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  1051. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  1052. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  1053. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  1054. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  1055. }
  1056. static int rs_tiocmset(struct tty_struct *tty, struct file *file,
  1057. unsigned int set, unsigned int clear)
  1058. {
  1059. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1060. unsigned long flags;
  1061. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1062. return -ENODEV;
  1063. if (tty->flags & (1 << TTY_IO_ERROR))
  1064. return -EIO;
  1065. local_irq_save(flags);
  1066. if (set & TIOCM_RTS)
  1067. info->MCR |= SER_RTS;
  1068. if (set & TIOCM_DTR)
  1069. info->MCR |= SER_DTR;
  1070. if (clear & TIOCM_RTS)
  1071. info->MCR &= ~SER_RTS;
  1072. if (clear & TIOCM_DTR)
  1073. info->MCR &= ~SER_DTR;
  1074. rtsdtr_ctrl(info->MCR);
  1075. local_irq_restore(flags);
  1076. return 0;
  1077. }
  1078. /*
  1079. * rs_break() --- routine which turns the break handling on or off
  1080. */
  1081. static void rs_break(struct tty_struct *tty, int break_state)
  1082. {
  1083. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1084. unsigned long flags;
  1085. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1086. return;
  1087. local_irq_save(flags);
  1088. if (break_state == -1)
  1089. custom.adkcon = AC_SETCLR | AC_UARTBRK;
  1090. else
  1091. custom.adkcon = AC_UARTBRK;
  1092. mb();
  1093. local_irq_restore(flags);
  1094. }
  1095. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1096. unsigned int cmd, unsigned long arg)
  1097. {
  1098. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1099. struct async_icount cprev, cnow; /* kernel counter temps */
  1100. struct serial_icounter_struct icount;
  1101. unsigned long flags;
  1102. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1103. return -ENODEV;
  1104. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1105. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1106. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1107. if (tty->flags & (1 << TTY_IO_ERROR))
  1108. return -EIO;
  1109. }
  1110. switch (cmd) {
  1111. case TIOCGSERIAL:
  1112. return get_serial_info(info,
  1113. (struct serial_struct *) arg);
  1114. case TIOCSSERIAL:
  1115. return set_serial_info(info,
  1116. (struct serial_struct *) arg);
  1117. case TIOCSERCONFIG:
  1118. return 0;
  1119. case TIOCSERGETLSR: /* Get line status register */
  1120. return get_lsr_info(info, (unsigned int *) arg);
  1121. case TIOCSERGSTRUCT:
  1122. if (copy_to_user((struct async_struct *) arg,
  1123. info, sizeof(struct async_struct)))
  1124. return -EFAULT;
  1125. return 0;
  1126. /*
  1127. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1128. * - mask passed in arg for lines of interest
  1129. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1130. * Caller should use TIOCGICOUNT to see which one it was
  1131. */
  1132. case TIOCMIWAIT:
  1133. local_irq_save(flags);
  1134. /* note the counters on entry */
  1135. cprev = info->state->icount;
  1136. local_irq_restore(flags);
  1137. while (1) {
  1138. interruptible_sleep_on(&info->delta_msr_wait);
  1139. /* see if a signal did it */
  1140. if (signal_pending(current))
  1141. return -ERESTARTSYS;
  1142. local_irq_save(flags);
  1143. cnow = info->state->icount; /* atomic copy */
  1144. local_irq_restore(flags);
  1145. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1146. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1147. return -EIO; /* no change => error */
  1148. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1149. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1150. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1151. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1152. return 0;
  1153. }
  1154. cprev = cnow;
  1155. }
  1156. /* NOTREACHED */
  1157. /*
  1158. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1159. * Return: write counters to the user passed counter struct
  1160. * NB: both 1->0 and 0->1 transitions are counted except for
  1161. * RI where only 0->1 is counted.
  1162. */
  1163. case TIOCGICOUNT:
  1164. local_irq_save(flags);
  1165. cnow = info->state->icount;
  1166. local_irq_restore(flags);
  1167. icount.cts = cnow.cts;
  1168. icount.dsr = cnow.dsr;
  1169. icount.rng = cnow.rng;
  1170. icount.dcd = cnow.dcd;
  1171. icount.rx = cnow.rx;
  1172. icount.tx = cnow.tx;
  1173. icount.frame = cnow.frame;
  1174. icount.overrun = cnow.overrun;
  1175. icount.parity = cnow.parity;
  1176. icount.brk = cnow.brk;
  1177. icount.buf_overrun = cnow.buf_overrun;
  1178. if (copy_to_user((void *)arg, &icount, sizeof(icount)))
  1179. return -EFAULT;
  1180. return 0;
  1181. case TIOCSERGWILD:
  1182. case TIOCSERSWILD:
  1183. /* "setserial -W" is called in Debian boot */
  1184. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1185. return 0;
  1186. default:
  1187. return -ENOIOCTLCMD;
  1188. }
  1189. return 0;
  1190. }
  1191. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1192. {
  1193. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1194. unsigned long flags;
  1195. unsigned int cflag = tty->termios->c_cflag;
  1196. if ( (cflag == old_termios->c_cflag)
  1197. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  1198. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1199. return;
  1200. change_speed(info, old_termios);
  1201. /* Handle transition to B0 status */
  1202. if ((old_termios->c_cflag & CBAUD) &&
  1203. !(cflag & CBAUD)) {
  1204. info->MCR &= ~(SER_DTR|SER_RTS);
  1205. local_irq_save(flags);
  1206. rtsdtr_ctrl(info->MCR);
  1207. local_irq_restore(flags);
  1208. }
  1209. /* Handle transition away from B0 status */
  1210. if (!(old_termios->c_cflag & CBAUD) &&
  1211. (cflag & CBAUD)) {
  1212. info->MCR |= SER_DTR;
  1213. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1214. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1215. info->MCR |= SER_RTS;
  1216. }
  1217. local_irq_save(flags);
  1218. rtsdtr_ctrl(info->MCR);
  1219. local_irq_restore(flags);
  1220. }
  1221. /* Handle turning off CRTSCTS */
  1222. if ((old_termios->c_cflag & CRTSCTS) &&
  1223. !(tty->termios->c_cflag & CRTSCTS)) {
  1224. tty->hw_stopped = 0;
  1225. rs_start(tty);
  1226. }
  1227. #if 0
  1228. /*
  1229. * No need to wake up processes in open wait, since they
  1230. * sample the CLOCAL flag once, and don't recheck it.
  1231. * XXX It's not clear whether the current behavior is correct
  1232. * or not. Hence, this may change.....
  1233. */
  1234. if (!(old_termios->c_cflag & CLOCAL) &&
  1235. (tty->termios->c_cflag & CLOCAL))
  1236. wake_up_interruptible(&info->open_wait);
  1237. #endif
  1238. }
  1239. /*
  1240. * ------------------------------------------------------------
  1241. * rs_close()
  1242. *
  1243. * This routine is called when the serial port gets closed. First, we
  1244. * wait for the last remaining data to be sent. Then, we unlink its
  1245. * async structure from the interrupt chain if necessary, and we free
  1246. * that IRQ if nothing is left in the chain.
  1247. * ------------------------------------------------------------
  1248. */
  1249. static void rs_close(struct tty_struct *tty, struct file * filp)
  1250. {
  1251. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1252. struct serial_state *state;
  1253. unsigned long flags;
  1254. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1255. return;
  1256. state = info->state;
  1257. local_irq_save(flags);
  1258. if (tty_hung_up_p(filp)) {
  1259. DBG_CNT("before DEC-hung");
  1260. local_irq_restore(flags);
  1261. return;
  1262. }
  1263. #ifdef SERIAL_DEBUG_OPEN
  1264. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1265. #endif
  1266. if ((tty->count == 1) && (state->count != 1)) {
  1267. /*
  1268. * Uh, oh. tty->count is 1, which means that the tty
  1269. * structure will be freed. state->count should always
  1270. * be one in these conditions. If it's greater than
  1271. * one, we've got real problems, since it means the
  1272. * serial port won't be shutdown.
  1273. */
  1274. printk("rs_close: bad serial port count; tty->count is 1, "
  1275. "state->count is %d\n", state->count);
  1276. state->count = 1;
  1277. }
  1278. if (--state->count < 0) {
  1279. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1280. info->line, state->count);
  1281. state->count = 0;
  1282. }
  1283. if (state->count) {
  1284. DBG_CNT("before DEC-2");
  1285. local_irq_restore(flags);
  1286. return;
  1287. }
  1288. info->flags |= ASYNC_CLOSING;
  1289. /*
  1290. * Now we wait for the transmit buffer to clear; and we notify
  1291. * the line discipline to only process XON/XOFF characters.
  1292. */
  1293. tty->closing = 1;
  1294. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1295. tty_wait_until_sent(tty, info->closing_wait);
  1296. /*
  1297. * At this point we stop accepting input. To do this, we
  1298. * disable the receive line status interrupts, and tell the
  1299. * interrupt driver to stop checking the data ready bit in the
  1300. * line status register.
  1301. */
  1302. info->read_status_mask &= ~UART_LSR_DR;
  1303. if (info->flags & ASYNC_INITIALIZED) {
  1304. /* disable receive interrupts */
  1305. custom.intena = IF_RBF;
  1306. mb();
  1307. /* clear any pending receive interrupt */
  1308. custom.intreq = IF_RBF;
  1309. mb();
  1310. /*
  1311. * Before we drop DTR, make sure the UART transmitter
  1312. * has completely drained; this is especially
  1313. * important if there is a transmit FIFO!
  1314. */
  1315. rs_wait_until_sent(tty, info->timeout);
  1316. }
  1317. shutdown(info);
  1318. if (tty->driver->flush_buffer)
  1319. tty->driver->flush_buffer(tty);
  1320. tty_ldisc_flush(tty);
  1321. tty->closing = 0;
  1322. info->event = 0;
  1323. info->tty = NULL;
  1324. if (info->blocked_open) {
  1325. if (info->close_delay) {
  1326. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1327. }
  1328. wake_up_interruptible(&info->open_wait);
  1329. }
  1330. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1331. wake_up_interruptible(&info->close_wait);
  1332. local_irq_restore(flags);
  1333. }
  1334. /*
  1335. * rs_wait_until_sent() --- wait until the transmitter is empty
  1336. */
  1337. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1338. {
  1339. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1340. unsigned long orig_jiffies, char_time;
  1341. int lsr;
  1342. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1343. return;
  1344. if (info->xmit_fifo_size == 0)
  1345. return; /* Just in case.... */
  1346. orig_jiffies = jiffies;
  1347. /*
  1348. * Set the check interval to be 1/5 of the estimated time to
  1349. * send a single character, and make it at least 1. The check
  1350. * interval should also be less than the timeout.
  1351. *
  1352. * Note: we have to use pretty tight timings here to satisfy
  1353. * the NIST-PCTS.
  1354. */
  1355. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1356. char_time = char_time / 5;
  1357. if (char_time == 0)
  1358. char_time = 1;
  1359. if (timeout)
  1360. char_time = min_t(unsigned long, char_time, timeout);
  1361. /*
  1362. * If the transmitter hasn't cleared in twice the approximate
  1363. * amount of time to send the entire FIFO, it probably won't
  1364. * ever clear. This assumes the UART isn't doing flow
  1365. * control, which is currently the case. Hence, if it ever
  1366. * takes longer than info->timeout, this is probably due to a
  1367. * UART bug of some kind. So, we clamp the timeout parameter at
  1368. * 2*info->timeout.
  1369. */
  1370. if (!timeout || timeout > 2*info->timeout)
  1371. timeout = 2*info->timeout;
  1372. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1373. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1374. printk("jiff=%lu...", jiffies);
  1375. #endif
  1376. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1377. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1378. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1379. #endif
  1380. msleep_interruptible(jiffies_to_msecs(char_time));
  1381. if (signal_pending(current))
  1382. break;
  1383. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1384. break;
  1385. }
  1386. current->state = TASK_RUNNING;
  1387. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1388. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1389. #endif
  1390. }
  1391. /*
  1392. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1393. */
  1394. static void rs_hangup(struct tty_struct *tty)
  1395. {
  1396. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1397. struct serial_state *state = info->state;
  1398. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1399. return;
  1400. state = info->state;
  1401. rs_flush_buffer(tty);
  1402. shutdown(info);
  1403. info->event = 0;
  1404. state->count = 0;
  1405. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1406. info->tty = NULL;
  1407. wake_up_interruptible(&info->open_wait);
  1408. }
  1409. /*
  1410. * ------------------------------------------------------------
  1411. * rs_open() and friends
  1412. * ------------------------------------------------------------
  1413. */
  1414. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1415. struct async_struct *info)
  1416. {
  1417. #ifdef DECLARE_WAITQUEUE
  1418. DECLARE_WAITQUEUE(wait, current);
  1419. #else
  1420. struct wait_queue wait = { current, NULL };
  1421. #endif
  1422. struct serial_state *state = info->state;
  1423. int retval;
  1424. int do_clocal = 0, extra_count = 0;
  1425. unsigned long flags;
  1426. /*
  1427. * If the device is in the middle of being closed, then block
  1428. * until it's done, and then try again.
  1429. */
  1430. if (tty_hung_up_p(filp) ||
  1431. (info->flags & ASYNC_CLOSING)) {
  1432. if (info->flags & ASYNC_CLOSING)
  1433. interruptible_sleep_on(&info->close_wait);
  1434. #ifdef SERIAL_DO_RESTART
  1435. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1436. -EAGAIN : -ERESTARTSYS);
  1437. #else
  1438. return -EAGAIN;
  1439. #endif
  1440. }
  1441. /*
  1442. * If non-blocking mode is set, or the port is not enabled,
  1443. * then make the check up front and then exit.
  1444. */
  1445. if ((filp->f_flags & O_NONBLOCK) ||
  1446. (tty->flags & (1 << TTY_IO_ERROR))) {
  1447. info->flags |= ASYNC_NORMAL_ACTIVE;
  1448. return 0;
  1449. }
  1450. if (tty->termios->c_cflag & CLOCAL)
  1451. do_clocal = 1;
  1452. /*
  1453. * Block waiting for the carrier detect and the line to become
  1454. * free (i.e., not in use by the callout). While we are in
  1455. * this loop, state->count is dropped by one, so that
  1456. * rs_close() knows when to free things. We restore it upon
  1457. * exit, either normal or abnormal.
  1458. */
  1459. retval = 0;
  1460. add_wait_queue(&info->open_wait, &wait);
  1461. #ifdef SERIAL_DEBUG_OPEN
  1462. printk("block_til_ready before block: ttys%d, count = %d\n",
  1463. state->line, state->count);
  1464. #endif
  1465. local_irq_save(flags);
  1466. if (!tty_hung_up_p(filp)) {
  1467. extra_count = 1;
  1468. state->count--;
  1469. }
  1470. local_irq_restore(flags);
  1471. info->blocked_open++;
  1472. while (1) {
  1473. local_irq_save(flags);
  1474. if (tty->termios->c_cflag & CBAUD)
  1475. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1476. local_irq_restore(flags);
  1477. set_current_state(TASK_INTERRUPTIBLE);
  1478. if (tty_hung_up_p(filp) ||
  1479. !(info->flags & ASYNC_INITIALIZED)) {
  1480. #ifdef SERIAL_DO_RESTART
  1481. if (info->flags & ASYNC_HUP_NOTIFY)
  1482. retval = -EAGAIN;
  1483. else
  1484. retval = -ERESTARTSYS;
  1485. #else
  1486. retval = -EAGAIN;
  1487. #endif
  1488. break;
  1489. }
  1490. if (!(info->flags & ASYNC_CLOSING) &&
  1491. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1492. break;
  1493. if (signal_pending(current)) {
  1494. retval = -ERESTARTSYS;
  1495. break;
  1496. }
  1497. #ifdef SERIAL_DEBUG_OPEN
  1498. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1499. info->line, state->count);
  1500. #endif
  1501. schedule();
  1502. }
  1503. current->state = TASK_RUNNING;
  1504. remove_wait_queue(&info->open_wait, &wait);
  1505. if (extra_count)
  1506. state->count++;
  1507. info->blocked_open--;
  1508. #ifdef SERIAL_DEBUG_OPEN
  1509. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1510. info->line, state->count);
  1511. #endif
  1512. if (retval)
  1513. return retval;
  1514. info->flags |= ASYNC_NORMAL_ACTIVE;
  1515. return 0;
  1516. }
  1517. static int get_async_struct(int line, struct async_struct **ret_info)
  1518. {
  1519. struct async_struct *info;
  1520. struct serial_state *sstate;
  1521. sstate = rs_table + line;
  1522. sstate->count++;
  1523. if (sstate->info) {
  1524. *ret_info = sstate->info;
  1525. return 0;
  1526. }
  1527. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  1528. if (!info) {
  1529. sstate->count--;
  1530. return -ENOMEM;
  1531. }
  1532. memset(info, 0, sizeof(struct async_struct));
  1533. #ifdef DECLARE_WAITQUEUE
  1534. init_waitqueue_head(&info->open_wait);
  1535. init_waitqueue_head(&info->close_wait);
  1536. init_waitqueue_head(&info->delta_msr_wait);
  1537. #endif
  1538. info->magic = SERIAL_MAGIC;
  1539. info->port = sstate->port;
  1540. info->flags = sstate->flags;
  1541. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1542. info->line = line;
  1543. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1544. info->state = sstate;
  1545. if (sstate->info) {
  1546. kfree(info);
  1547. *ret_info = sstate->info;
  1548. return 0;
  1549. }
  1550. *ret_info = sstate->info = info;
  1551. return 0;
  1552. }
  1553. /*
  1554. * This routine is called whenever a serial port is opened. It
  1555. * enables interrupts for a serial port, linking in its async structure into
  1556. * the IRQ chain. It also performs the serial-specific
  1557. * initialization for the tty structure.
  1558. */
  1559. static int rs_open(struct tty_struct *tty, struct file * filp)
  1560. {
  1561. struct async_struct *info;
  1562. int retval, line;
  1563. unsigned long page;
  1564. line = tty->index;
  1565. if ((line < 0) || (line >= NR_PORTS)) {
  1566. return -ENODEV;
  1567. }
  1568. retval = get_async_struct(line, &info);
  1569. if (retval) {
  1570. return retval;
  1571. }
  1572. tty->driver_data = info;
  1573. info->tty = tty;
  1574. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1575. return -ENODEV;
  1576. #ifdef SERIAL_DEBUG_OPEN
  1577. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1578. #endif
  1579. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1580. if (!tmp_buf) {
  1581. page = get_zeroed_page(GFP_KERNEL);
  1582. if (!page) {
  1583. return -ENOMEM;
  1584. }
  1585. if (tmp_buf)
  1586. free_page(page);
  1587. else
  1588. tmp_buf = (unsigned char *) page;
  1589. }
  1590. /*
  1591. * If the port is the middle of closing, bail out now
  1592. */
  1593. if (tty_hung_up_p(filp) ||
  1594. (info->flags & ASYNC_CLOSING)) {
  1595. if (info->flags & ASYNC_CLOSING)
  1596. interruptible_sleep_on(&info->close_wait);
  1597. #ifdef SERIAL_DO_RESTART
  1598. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1599. -EAGAIN : -ERESTARTSYS);
  1600. #else
  1601. return -EAGAIN;
  1602. #endif
  1603. }
  1604. /*
  1605. * Start up serial port
  1606. */
  1607. retval = startup(info);
  1608. if (retval) {
  1609. return retval;
  1610. }
  1611. retval = block_til_ready(tty, filp, info);
  1612. if (retval) {
  1613. #ifdef SERIAL_DEBUG_OPEN
  1614. printk("rs_open returning after block_til_ready with %d\n",
  1615. retval);
  1616. #endif
  1617. return retval;
  1618. }
  1619. #ifdef SERIAL_DEBUG_OPEN
  1620. printk("rs_open %s successful...", tty->name);
  1621. #endif
  1622. return 0;
  1623. }
  1624. /*
  1625. * /proc fs routines....
  1626. */
  1627. static inline int line_info(char *buf, struct serial_state *state)
  1628. {
  1629. struct async_struct *info = state->info, scr_info;
  1630. char stat_buf[30], control, status;
  1631. int ret;
  1632. unsigned long flags;
  1633. ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
  1634. /*
  1635. * Figure out the current RS-232 lines
  1636. */
  1637. if (!info) {
  1638. info = &scr_info; /* This is just for serial_{in,out} */
  1639. info->magic = SERIAL_MAGIC;
  1640. info->flags = state->flags;
  1641. info->quot = 0;
  1642. info->tty = NULL;
  1643. }
  1644. local_irq_save(flags);
  1645. status = ciab.pra;
  1646. control = info ? info->MCR : status;
  1647. local_irq_restore(flags);
  1648. stat_buf[0] = 0;
  1649. stat_buf[1] = 0;
  1650. if(!(control & SER_RTS))
  1651. strcat(stat_buf, "|RTS");
  1652. if(!(status & SER_CTS))
  1653. strcat(stat_buf, "|CTS");
  1654. if(!(control & SER_DTR))
  1655. strcat(stat_buf, "|DTR");
  1656. if(!(status & SER_DSR))
  1657. strcat(stat_buf, "|DSR");
  1658. if(!(status & SER_DCD))
  1659. strcat(stat_buf, "|CD");
  1660. if (info->quot) {
  1661. ret += sprintf(buf+ret, " baud:%d",
  1662. state->baud_base / info->quot);
  1663. }
  1664. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1665. state->icount.tx, state->icount.rx);
  1666. if (state->icount.frame)
  1667. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1668. if (state->icount.parity)
  1669. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1670. if (state->icount.brk)
  1671. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1672. if (state->icount.overrun)
  1673. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1674. /*
  1675. * Last thing is the RS-232 status lines
  1676. */
  1677. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1678. return ret;
  1679. }
  1680. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1681. int *eof, void *data)
  1682. {
  1683. int len = 0, l;
  1684. off_t begin = 0;
  1685. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  1686. l = line_info(page + len, &rs_table[0]);
  1687. len += l;
  1688. if (len+begin > off+count)
  1689. goto done;
  1690. if (len+begin < off) {
  1691. begin += len;
  1692. len = 0;
  1693. }
  1694. *eof = 1;
  1695. done:
  1696. if (off >= len+begin)
  1697. return 0;
  1698. *start = page + (off-begin);
  1699. return ((count < begin+len-off) ? count : begin+len-off);
  1700. }
  1701. /*
  1702. * ---------------------------------------------------------------------
  1703. * rs_init() and friends
  1704. *
  1705. * rs_init() is called at boot-time to initialize the serial driver.
  1706. * ---------------------------------------------------------------------
  1707. */
  1708. /*
  1709. * This routine prints out the appropriate serial driver version
  1710. * number, and identifies which options were configured into this
  1711. * driver.
  1712. */
  1713. static _INLINE_ void show_serial_version(void)
  1714. {
  1715. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1716. }
  1717. static struct tty_operations serial_ops = {
  1718. .open = rs_open,
  1719. .close = rs_close,
  1720. .write = rs_write,
  1721. .put_char = rs_put_char,
  1722. .flush_chars = rs_flush_chars,
  1723. .write_room = rs_write_room,
  1724. .chars_in_buffer = rs_chars_in_buffer,
  1725. .flush_buffer = rs_flush_buffer,
  1726. .ioctl = rs_ioctl,
  1727. .throttle = rs_throttle,
  1728. .unthrottle = rs_unthrottle,
  1729. .set_termios = rs_set_termios,
  1730. .stop = rs_stop,
  1731. .start = rs_start,
  1732. .hangup = rs_hangup,
  1733. .break_ctl = rs_break,
  1734. .send_xchar = rs_send_xchar,
  1735. .wait_until_sent = rs_wait_until_sent,
  1736. .read_proc = rs_read_proc,
  1737. .tiocmget = rs_tiocmget,
  1738. .tiocmset = rs_tiocmset,
  1739. };
  1740. /*
  1741. * The serial driver boot-time initialization code!
  1742. */
  1743. static int __init rs_init(void)
  1744. {
  1745. unsigned long flags;
  1746. struct serial_state * state;
  1747. if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
  1748. return -ENODEV;
  1749. serial_driver = alloc_tty_driver(1);
  1750. if (!serial_driver)
  1751. return -ENOMEM;
  1752. /*
  1753. * We request SERDAT and SERPER only, because the serial registers are
  1754. * too spreaded over the custom register space
  1755. */
  1756. if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
  1757. return -EBUSY;
  1758. IRQ_ports = NULL;
  1759. show_serial_version();
  1760. /* Initialize the tty_driver structure */
  1761. serial_driver->owner = THIS_MODULE;
  1762. serial_driver->driver_name = "amiserial";
  1763. serial_driver->name = "ttyS";
  1764. serial_driver->major = TTY_MAJOR;
  1765. serial_driver->minor_start = 64;
  1766. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1767. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1768. serial_driver->init_termios = tty_std_termios;
  1769. serial_driver->init_termios.c_cflag =
  1770. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1771. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1772. tty_set_operations(serial_driver, &serial_ops);
  1773. if (tty_register_driver(serial_driver))
  1774. panic("Couldn't register serial driver\n");
  1775. state = rs_table;
  1776. state->magic = SSTATE_MAGIC;
  1777. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1778. state->line = 0;
  1779. state->custom_divisor = 0;
  1780. state->close_delay = 5*HZ/10;
  1781. state->closing_wait = 30*HZ;
  1782. state->icount.cts = state->icount.dsr =
  1783. state->icount.rng = state->icount.dcd = 0;
  1784. state->icount.rx = state->icount.tx = 0;
  1785. state->icount.frame = state->icount.parity = 0;
  1786. state->icount.overrun = state->icount.brk = 0;
  1787. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1788. state->line);
  1789. /* Hardware set up */
  1790. state->baud_base = amiga_colorclock;
  1791. state->xmit_fifo_size = 1;
  1792. local_irq_save(flags);
  1793. /* set ISRs, and then disable the rx interrupts */
  1794. request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1795. request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
  1796. /* turn off Rx and Tx interrupts */
  1797. custom.intena = IF_RBF | IF_TBE;
  1798. mb();
  1799. /* clear any pending interrupt */
  1800. custom.intreq = IF_RBF | IF_TBE;
  1801. mb();
  1802. local_irq_restore(flags);
  1803. /*
  1804. * set the appropriate directions for the modem control flags,
  1805. * and clear RTS and DTR
  1806. */
  1807. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1808. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1809. return 0;
  1810. }
  1811. static __exit void rs_exit(void)
  1812. {
  1813. int error;
  1814. struct async_struct *info = rs_table[0].info;
  1815. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1816. tasklet_kill(&info->tlet);
  1817. if ((error = tty_unregister_driver(serial_driver)))
  1818. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1819. error);
  1820. put_tty_driver(serial_driver);
  1821. if (info) {
  1822. rs_table[0].info = NULL;
  1823. kfree(info);
  1824. }
  1825. if (tmp_buf) {
  1826. free_page((unsigned long) tmp_buf);
  1827. tmp_buf = NULL;
  1828. }
  1829. release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
  1830. }
  1831. module_init(rs_init)
  1832. module_exit(rs_exit)
  1833. /*
  1834. * ------------------------------------------------------------
  1835. * Serial console driver
  1836. * ------------------------------------------------------------
  1837. */
  1838. #ifdef CONFIG_SERIAL_CONSOLE
  1839. static void amiga_serial_putc(char c)
  1840. {
  1841. custom.serdat = (unsigned char)c | 0x100;
  1842. while (!(custom.serdatr & 0x2000))
  1843. barrier();
  1844. }
  1845. /*
  1846. * Print a string to the serial port trying not to disturb
  1847. * any possible real use of the port...
  1848. *
  1849. * The console must be locked when we get here.
  1850. */
  1851. static void serial_console_write(struct console *co, const char *s,
  1852. unsigned count)
  1853. {
  1854. unsigned short intena = custom.intenar;
  1855. custom.intena = IF_TBE;
  1856. while (count--) {
  1857. if (*s == '\n')
  1858. amiga_serial_putc('\r');
  1859. amiga_serial_putc(*s++);
  1860. }
  1861. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1862. }
  1863. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1864. {
  1865. *index = 0;
  1866. return serial_driver;
  1867. }
  1868. static struct console sercons = {
  1869. .name = "ttyS",
  1870. .write = serial_console_write,
  1871. .device = serial_console_device,
  1872. .flags = CON_PRINTBUFFER,
  1873. .index = -1,
  1874. };
  1875. /*
  1876. * Register console.
  1877. */
  1878. static int __init amiserial_console_init(void)
  1879. {
  1880. register_console(&sercons);
  1881. return 0;
  1882. }
  1883. console_initcall(amiserial_console_init);
  1884. #endif
  1885. MODULE_LICENSE("GPL");