amiserial.c 45 KB

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