amiserial.c 54 KB

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