amiserial.c 45 KB

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