amiserial.c 53 KB

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