amiserial.c 46 KB

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