amiserial.c 50 KB

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