amiserial.c 40 KB

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