amiserial.c 45 KB

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