amiserial.c 42 KB

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