amiserial.c 46 KB

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