con3215.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * drivers/s390/char/con3215.c
  3. * 3215 line mode terminal driver.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  8. *
  9. * Updated:
  10. * Aug-2000: Added tab support
  11. * Dan Morrison, IBM Corporation (dmorriso@cse.buffalo.edu)
  12. */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/tty.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/vt_kern.h>
  20. #include <linux/init.h>
  21. #include <linux/console.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/bootmem.h>
  26. #include <asm/ccwdev.h>
  27. #include <asm/cio.h>
  28. #include <asm/io.h>
  29. #include <asm/ebcdic.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/delay.h>
  32. #include <asm/cpcmd.h>
  33. #include <asm/setup.h>
  34. #include "ctrlchar.h"
  35. #define NR_3215 1
  36. #define NR_3215_REQ (4*NR_3215)
  37. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  38. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  39. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  40. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  41. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  42. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  43. #define RAW3215_NR_CCWS 3
  44. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  45. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  46. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  47. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  48. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  49. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  50. #define RAW3215_CLOSING 32 /* set while in close process */
  51. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  52. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  53. #define TAB_STOP_SIZE 8 /* tab stop size */
  54. /*
  55. * Request types for a 3215 device
  56. */
  57. enum raw3215_type {
  58. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  59. };
  60. /*
  61. * Request structure for a 3215 device
  62. */
  63. struct raw3215_req {
  64. enum raw3215_type type; /* type of the request */
  65. int start, len; /* start index & len in output buffer */
  66. int delayable; /* indication to wait for more data */
  67. int residual; /* residual count for read request */
  68. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  69. struct raw3215_info *info; /* pointer to main structure */
  70. struct raw3215_req *next; /* pointer to next request */
  71. } __attribute__ ((aligned(8)));
  72. struct raw3215_info {
  73. struct ccw_device *cdev; /* device for tty driver */
  74. spinlock_t *lock; /* pointer to irq lock */
  75. int flags; /* state flags */
  76. char *buffer; /* pointer to output buffer */
  77. char *inbuf; /* pointer to input buffer */
  78. int head; /* first free byte in output buffer */
  79. int count; /* number of bytes in output buffer */
  80. int written; /* number of bytes in write requests */
  81. struct tty_struct *tty; /* pointer to tty structure if present */
  82. struct tasklet_struct tasklet;
  83. struct raw3215_req *queued_read; /* pointer to queued read requests */
  84. struct raw3215_req *queued_write;/* pointer to queued write requests */
  85. wait_queue_head_t empty_wait; /* wait queue for flushing */
  86. struct timer_list timer; /* timer for delayed output */
  87. char *message; /* pending message from raw3215_irq */
  88. int msg_dstat; /* dstat for pending message */
  89. int msg_cstat; /* cstat for pending message */
  90. int line_pos; /* position on the line (for tabs) */
  91. char ubuffer[80]; /* copy_from_user buffer */
  92. };
  93. /* array of 3215 devices structures */
  94. static struct raw3215_info *raw3215[NR_3215];
  95. /* spinlock to protect the raw3215 array */
  96. static DEFINE_SPINLOCK(raw3215_device_lock);
  97. /* list of free request structures */
  98. static struct raw3215_req *raw3215_freelist;
  99. /* spinlock to protect free list */
  100. static spinlock_t raw3215_freelist_lock;
  101. static struct tty_driver *tty3215_driver;
  102. /*
  103. * Get a request structure from the free list
  104. */
  105. static inline struct raw3215_req *
  106. raw3215_alloc_req(void) {
  107. struct raw3215_req *req;
  108. unsigned long flags;
  109. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  110. req = raw3215_freelist;
  111. raw3215_freelist = req->next;
  112. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  113. return req;
  114. }
  115. /*
  116. * Put a request structure back to the free list
  117. */
  118. static inline void
  119. raw3215_free_req(struct raw3215_req *req) {
  120. unsigned long flags;
  121. if (req->type == RAW3215_FREE)
  122. return; /* don't free a free request */
  123. req->type = RAW3215_FREE;
  124. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  125. req->next = raw3215_freelist;
  126. raw3215_freelist = req;
  127. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  128. }
  129. /*
  130. * Set up a read request that reads up to 160 byte from the 3215 device.
  131. * If there is a queued read request it is used, but that shouldn't happen
  132. * because a 3215 terminal won't accept a new read before the old one is
  133. * completed.
  134. */
  135. static void
  136. raw3215_mk_read_req(struct raw3215_info *raw)
  137. {
  138. struct raw3215_req *req;
  139. struct ccw1 *ccw;
  140. /* there can only be ONE read request at a time */
  141. req = raw->queued_read;
  142. if (req == NULL) {
  143. /* no queued read request, use new req structure */
  144. req = raw3215_alloc_req();
  145. req->type = RAW3215_READ;
  146. req->info = raw;
  147. raw->queued_read = req;
  148. }
  149. ccw = req->ccws;
  150. ccw->cmd_code = 0x0A; /* read inquiry */
  151. ccw->flags = 0x20; /* ignore incorrect length */
  152. ccw->count = 160;
  153. ccw->cda = (__u32) __pa(raw->inbuf);
  154. }
  155. /*
  156. * Set up a write request with the information from the main structure.
  157. * A ccw chain is created that writes as much as possible from the output
  158. * buffer to the 3215 device. If a queued write exists it is replaced by
  159. * the new, probably lengthened request.
  160. */
  161. static void
  162. raw3215_mk_write_req(struct raw3215_info *raw)
  163. {
  164. struct raw3215_req *req;
  165. struct ccw1 *ccw;
  166. int len, count, ix, lines;
  167. if (raw->count <= raw->written)
  168. return;
  169. /* check if there is a queued write request */
  170. req = raw->queued_write;
  171. if (req == NULL) {
  172. /* no queued write request, use new req structure */
  173. req = raw3215_alloc_req();
  174. req->type = RAW3215_WRITE;
  175. req->info = raw;
  176. raw->queued_write = req;
  177. } else {
  178. raw->written -= req->len;
  179. }
  180. ccw = req->ccws;
  181. req->start = (raw->head - raw->count + raw->written) &
  182. (RAW3215_BUFFER_SIZE - 1);
  183. /*
  184. * now we have to count newlines. We can at max accept
  185. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  186. * a restriction in VM
  187. */
  188. lines = 0;
  189. ix = req->start;
  190. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  191. if (raw->buffer[ix] == 0x15)
  192. lines++;
  193. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  194. }
  195. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  196. if (len > RAW3215_MAX_BYTES)
  197. len = RAW3215_MAX_BYTES;
  198. req->len = len;
  199. raw->written += len;
  200. /* set the indication if we should try to enlarge this request */
  201. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  202. ix = req->start;
  203. while (len > 0) {
  204. if (ccw > req->ccws)
  205. ccw[-1].flags |= 0x40; /* use command chaining */
  206. ccw->cmd_code = 0x01; /* write, auto carrier return */
  207. ccw->flags = 0x20; /* ignore incorrect length ind. */
  208. ccw->cda =
  209. (__u32) __pa(raw->buffer + ix);
  210. count = len;
  211. if (ix + count > RAW3215_BUFFER_SIZE)
  212. count = RAW3215_BUFFER_SIZE - ix;
  213. ccw->count = count;
  214. len -= count;
  215. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  216. ccw++;
  217. }
  218. /*
  219. * Add a NOP to the channel program. 3215 devices are purely
  220. * emulated and its much better to avoid the channel end
  221. * interrupt in this case.
  222. */
  223. if (ccw > req->ccws)
  224. ccw[-1].flags |= 0x40; /* use command chaining */
  225. ccw->cmd_code = 0x03; /* NOP */
  226. ccw->flags = 0;
  227. ccw->cda = 0;
  228. ccw->count = 1;
  229. }
  230. /*
  231. * Start a read or a write request
  232. */
  233. static void
  234. raw3215_start_io(struct raw3215_info *raw)
  235. {
  236. struct raw3215_req *req;
  237. int res;
  238. req = raw->queued_read;
  239. if (req != NULL &&
  240. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  241. /* dequeue request */
  242. raw->queued_read = NULL;
  243. res = ccw_device_start(raw->cdev, req->ccws,
  244. (unsigned long) req, 0, 0);
  245. if (res != 0) {
  246. /* do_IO failed, put request back to queue */
  247. raw->queued_read = req;
  248. } else {
  249. raw->flags |= RAW3215_WORKING;
  250. }
  251. }
  252. req = raw->queued_write;
  253. if (req != NULL &&
  254. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  255. /* dequeue request */
  256. raw->queued_write = NULL;
  257. res = ccw_device_start(raw->cdev, req->ccws,
  258. (unsigned long) req, 0, 0);
  259. if (res != 0) {
  260. /* do_IO failed, put request back to queue */
  261. raw->queued_write = req;
  262. } else {
  263. raw->flags |= RAW3215_WORKING;
  264. }
  265. }
  266. }
  267. /*
  268. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  269. */
  270. static void
  271. raw3215_timeout(unsigned long __data)
  272. {
  273. struct raw3215_info *raw = (struct raw3215_info *) __data;
  274. unsigned long flags;
  275. spin_lock_irqsave(raw->lock, flags);
  276. if (raw->flags & RAW3215_TIMER_RUNS) {
  277. del_timer(&raw->timer);
  278. raw->flags &= ~RAW3215_TIMER_RUNS;
  279. raw3215_mk_write_req(raw);
  280. raw3215_start_io(raw);
  281. }
  282. spin_unlock_irqrestore(raw->lock, flags);
  283. }
  284. /*
  285. * Function to conditionally start an IO. A read is started immediately,
  286. * a write is only started immediately if the flush flag is on or the
  287. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  288. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  289. */
  290. static inline void
  291. raw3215_try_io(struct raw3215_info *raw)
  292. {
  293. if (!(raw->flags & RAW3215_ACTIVE))
  294. return;
  295. if (raw->queued_read != NULL)
  296. raw3215_start_io(raw);
  297. else if (raw->queued_write != NULL) {
  298. if ((raw->queued_write->delayable == 0) ||
  299. (raw->flags & RAW3215_FLUSHING)) {
  300. /* execute write requests bigger than minimum size */
  301. raw3215_start_io(raw);
  302. if (raw->flags & RAW3215_TIMER_RUNS) {
  303. del_timer(&raw->timer);
  304. raw->flags &= ~RAW3215_TIMER_RUNS;
  305. }
  306. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  307. /* delay small writes */
  308. init_timer(&raw->timer);
  309. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  310. raw->timer.data = (unsigned long) raw;
  311. raw->timer.function = raw3215_timeout;
  312. add_timer(&raw->timer);
  313. raw->flags |= RAW3215_TIMER_RUNS;
  314. }
  315. }
  316. }
  317. /*
  318. * The bottom half handler routine for 3215 devices. It tries to start
  319. * the next IO and wakes up processes waiting on the tty.
  320. */
  321. static void
  322. raw3215_tasklet(void *data)
  323. {
  324. struct raw3215_info *raw;
  325. struct tty_struct *tty;
  326. unsigned long flags;
  327. raw = (struct raw3215_info *) data;
  328. spin_lock_irqsave(raw->lock, flags);
  329. raw3215_mk_write_req(raw);
  330. raw3215_try_io(raw);
  331. spin_unlock_irqrestore(raw->lock, flags);
  332. /* Check for pending message from raw3215_irq */
  333. if (raw->message != NULL) {
  334. printk(raw->message, raw->msg_dstat, raw->msg_cstat);
  335. raw->message = NULL;
  336. }
  337. tty = raw->tty;
  338. if (tty != NULL &&
  339. RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
  340. tty_wakeup(tty);
  341. }
  342. }
  343. /*
  344. * Interrupt routine, called from common io layer
  345. */
  346. static void
  347. raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  348. {
  349. struct raw3215_info *raw;
  350. struct raw3215_req *req;
  351. struct tty_struct *tty;
  352. int cstat, dstat;
  353. int count, slen;
  354. raw = cdev->dev.driver_data;
  355. req = (struct raw3215_req *) intparm;
  356. cstat = irb->scsw.cstat;
  357. dstat = irb->scsw.dstat;
  358. if (cstat != 0) {
  359. raw->message = KERN_WARNING
  360. "Got nonzero channel status in raw3215_irq "
  361. "(dev sts 0x%2x, sch sts 0x%2x)";
  362. raw->msg_dstat = dstat;
  363. raw->msg_cstat = cstat;
  364. tasklet_schedule(&raw->tasklet);
  365. }
  366. if (dstat & 0x01) { /* we got a unit exception */
  367. dstat &= ~0x01; /* we can ignore it */
  368. }
  369. switch (dstat) {
  370. case 0x80:
  371. if (cstat != 0)
  372. break;
  373. /* Attention interrupt, someone hit the enter key */
  374. raw3215_mk_read_req(raw);
  375. if (MACHINE_IS_P390)
  376. memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
  377. tasklet_schedule(&raw->tasklet);
  378. break;
  379. case 0x08:
  380. case 0x0C:
  381. /* Channel end interrupt. */
  382. if ((raw = req->info) == NULL)
  383. return; /* That shouldn't happen ... */
  384. if (req->type == RAW3215_READ) {
  385. /* store residual count, then wait for device end */
  386. req->residual = irb->scsw.count;
  387. }
  388. if (dstat == 0x08)
  389. break;
  390. case 0x04:
  391. /* Device end interrupt. */
  392. if ((raw = req->info) == NULL)
  393. return; /* That shouldn't happen ... */
  394. if (req->type == RAW3215_READ && raw->tty != NULL) {
  395. unsigned int cchar;
  396. tty = raw->tty;
  397. count = 160 - req->residual;
  398. if (MACHINE_IS_P390) {
  399. slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
  400. if (count > slen)
  401. count = slen;
  402. } else
  403. EBCASC(raw->inbuf, count);
  404. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  405. switch (cchar & CTRLCHAR_MASK) {
  406. case CTRLCHAR_SYSRQ:
  407. break;
  408. case CTRLCHAR_CTRL:
  409. tty_insert_flip_char(tty, cchar, TTY_NORMAL);
  410. tty_flip_buffer_push(raw->tty);
  411. break;
  412. case CTRLCHAR_NONE:
  413. if (count < 2 ||
  414. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  415. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  416. /* add the auto \n */
  417. raw->inbuf[count] = '\n';
  418. count++;
  419. } else
  420. count -= 2;
  421. tty_insert_flip_string(tty, raw->inbuf, count);
  422. tty_flip_buffer_push(raw->tty);
  423. break;
  424. }
  425. } else if (req->type == RAW3215_WRITE) {
  426. raw->count -= req->len;
  427. raw->written -= req->len;
  428. }
  429. raw->flags &= ~RAW3215_WORKING;
  430. raw3215_free_req(req);
  431. /* check for empty wait */
  432. if (waitqueue_active(&raw->empty_wait) &&
  433. raw->queued_write == NULL &&
  434. raw->queued_read == NULL) {
  435. wake_up_interruptible(&raw->empty_wait);
  436. }
  437. tasklet_schedule(&raw->tasklet);
  438. break;
  439. default:
  440. /* Strange interrupt, I'll do my best to clean up */
  441. if (req != NULL && req->type != RAW3215_FREE) {
  442. if (req->type == RAW3215_WRITE) {
  443. raw->count -= req->len;
  444. raw->written -= req->len;
  445. }
  446. raw->flags &= ~RAW3215_WORKING;
  447. raw3215_free_req(req);
  448. }
  449. raw->message = KERN_WARNING
  450. "Spurious interrupt in in raw3215_irq "
  451. "(dev sts 0x%2x, sch sts 0x%2x)";
  452. raw->msg_dstat = dstat;
  453. raw->msg_cstat = cstat;
  454. tasklet_schedule(&raw->tasklet);
  455. }
  456. return;
  457. }
  458. /*
  459. * Wait until length bytes are available int the output buffer.
  460. * Has to be called with the s390irq lock held. Can be called
  461. * disabled.
  462. */
  463. static void
  464. raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  465. {
  466. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  467. /* there might be a request pending */
  468. raw->flags |= RAW3215_FLUSHING;
  469. raw3215_mk_write_req(raw);
  470. raw3215_try_io(raw);
  471. raw->flags &= ~RAW3215_FLUSHING;
  472. #ifdef CONFIG_TN3215_CONSOLE
  473. wait_cons_dev();
  474. #endif
  475. /* Enough room freed up ? */
  476. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  477. break;
  478. /* there might be another cpu waiting for the lock */
  479. spin_unlock(raw->lock);
  480. udelay(100);
  481. spin_lock(raw->lock);
  482. }
  483. }
  484. /*
  485. * String write routine for 3215 devices
  486. */
  487. static void
  488. raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
  489. {
  490. unsigned long flags;
  491. int c, count;
  492. while (length > 0) {
  493. spin_lock_irqsave(raw->lock, flags);
  494. count = (length > RAW3215_BUFFER_SIZE) ?
  495. RAW3215_BUFFER_SIZE : length;
  496. length -= count;
  497. raw3215_make_room(raw, count);
  498. /* copy string to output buffer and convert it to EBCDIC */
  499. while (1) {
  500. c = min_t(int, count,
  501. min(RAW3215_BUFFER_SIZE - raw->count,
  502. RAW3215_BUFFER_SIZE - raw->head));
  503. if (c <= 0)
  504. break;
  505. memcpy(raw->buffer + raw->head, str, c);
  506. ASCEBC(raw->buffer + raw->head, c);
  507. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  508. raw->count += c;
  509. raw->line_pos += c;
  510. str += c;
  511. count -= c;
  512. }
  513. if (!(raw->flags & RAW3215_WORKING)) {
  514. raw3215_mk_write_req(raw);
  515. /* start or queue request */
  516. raw3215_try_io(raw);
  517. }
  518. spin_unlock_irqrestore(raw->lock, flags);
  519. }
  520. }
  521. /*
  522. * Put character routine for 3215 devices
  523. */
  524. static void
  525. raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  526. {
  527. unsigned long flags;
  528. unsigned int length, i;
  529. spin_lock_irqsave(raw->lock, flags);
  530. if (ch == '\t') {
  531. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  532. raw->line_pos += length;
  533. ch = ' ';
  534. } else if (ch == '\n') {
  535. length = 1;
  536. raw->line_pos = 0;
  537. } else {
  538. length = 1;
  539. raw->line_pos++;
  540. }
  541. raw3215_make_room(raw, length);
  542. for (i = 0; i < length; i++) {
  543. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  544. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  545. raw->count++;
  546. }
  547. if (!(raw->flags & RAW3215_WORKING)) {
  548. raw3215_mk_write_req(raw);
  549. /* start or queue request */
  550. raw3215_try_io(raw);
  551. }
  552. spin_unlock_irqrestore(raw->lock, flags);
  553. }
  554. /*
  555. * Flush routine, it simply sets the flush flag and tries to start
  556. * pending IO.
  557. */
  558. static void
  559. raw3215_flush_buffer(struct raw3215_info *raw)
  560. {
  561. unsigned long flags;
  562. spin_lock_irqsave(raw->lock, flags);
  563. if (raw->count > 0) {
  564. raw->flags |= RAW3215_FLUSHING;
  565. raw3215_try_io(raw);
  566. raw->flags &= ~RAW3215_FLUSHING;
  567. }
  568. spin_unlock_irqrestore(raw->lock, flags);
  569. }
  570. /*
  571. * Fire up a 3215 device.
  572. */
  573. static int
  574. raw3215_startup(struct raw3215_info *raw)
  575. {
  576. unsigned long flags;
  577. if (raw->flags & RAW3215_ACTIVE)
  578. return 0;
  579. raw->line_pos = 0;
  580. raw->flags |= RAW3215_ACTIVE;
  581. spin_lock_irqsave(raw->lock, flags);
  582. raw3215_try_io(raw);
  583. spin_unlock_irqrestore(raw->lock, flags);
  584. return 0;
  585. }
  586. /*
  587. * Shutdown a 3215 device.
  588. */
  589. static void
  590. raw3215_shutdown(struct raw3215_info *raw)
  591. {
  592. DECLARE_WAITQUEUE(wait, current);
  593. unsigned long flags;
  594. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  595. return;
  596. /* Wait for outstanding requests, then free irq */
  597. spin_lock_irqsave(raw->lock, flags);
  598. if ((raw->flags & RAW3215_WORKING) ||
  599. raw->queued_write != NULL ||
  600. raw->queued_read != NULL) {
  601. raw->flags |= RAW3215_CLOSING;
  602. add_wait_queue(&raw->empty_wait, &wait);
  603. set_current_state(TASK_INTERRUPTIBLE);
  604. spin_unlock_irqrestore(raw->lock, flags);
  605. schedule();
  606. spin_lock_irqsave(raw->lock, flags);
  607. remove_wait_queue(&raw->empty_wait, &wait);
  608. set_current_state(TASK_RUNNING);
  609. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  610. }
  611. spin_unlock_irqrestore(raw->lock, flags);
  612. }
  613. static int
  614. raw3215_probe (struct ccw_device *cdev)
  615. {
  616. struct raw3215_info *raw;
  617. int line;
  618. raw = kmalloc(sizeof(struct raw3215_info) +
  619. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  620. if (raw == NULL)
  621. return -ENOMEM;
  622. spin_lock(&raw3215_device_lock);
  623. for (line = 0; line < NR_3215; line++) {
  624. if (!raw3215[line]) {
  625. raw3215[line] = raw;
  626. break;
  627. }
  628. }
  629. spin_unlock(&raw3215_device_lock);
  630. if (line == NR_3215) {
  631. kfree(raw);
  632. return -ENODEV;
  633. }
  634. raw->cdev = cdev;
  635. raw->lock = get_ccwdev_lock(cdev);
  636. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  637. memset(raw, 0, sizeof(struct raw3215_info));
  638. raw->buffer = (char *) kmalloc(RAW3215_BUFFER_SIZE,
  639. GFP_KERNEL|GFP_DMA);
  640. if (raw->buffer == NULL) {
  641. spin_lock(&raw3215_device_lock);
  642. raw3215[line] = 0;
  643. spin_unlock(&raw3215_device_lock);
  644. kfree(raw);
  645. return -ENOMEM;
  646. }
  647. tasklet_init(&raw->tasklet,
  648. (void (*)(unsigned long)) raw3215_tasklet,
  649. (unsigned long) raw);
  650. init_waitqueue_head(&raw->empty_wait);
  651. cdev->dev.driver_data = raw;
  652. cdev->handler = raw3215_irq;
  653. return 0;
  654. }
  655. static void
  656. raw3215_remove (struct ccw_device *cdev)
  657. {
  658. struct raw3215_info *raw;
  659. ccw_device_set_offline(cdev);
  660. raw = cdev->dev.driver_data;
  661. if (raw) {
  662. cdev->dev.driver_data = NULL;
  663. kfree(raw->buffer);
  664. kfree(raw);
  665. }
  666. }
  667. static int
  668. raw3215_set_online (struct ccw_device *cdev)
  669. {
  670. struct raw3215_info *raw;
  671. raw = cdev->dev.driver_data;
  672. if (!raw)
  673. return -ENODEV;
  674. return raw3215_startup(raw);
  675. }
  676. static int
  677. raw3215_set_offline (struct ccw_device *cdev)
  678. {
  679. struct raw3215_info *raw;
  680. raw = cdev->dev.driver_data;
  681. if (!raw)
  682. return -ENODEV;
  683. raw3215_shutdown(raw);
  684. return 0;
  685. }
  686. static struct ccw_device_id raw3215_id[] = {
  687. { CCW_DEVICE(0x3215, 0) },
  688. { /* end of list */ },
  689. };
  690. static struct ccw_driver raw3215_ccw_driver = {
  691. .name = "3215",
  692. .owner = THIS_MODULE,
  693. .ids = raw3215_id,
  694. .probe = &raw3215_probe,
  695. .remove = &raw3215_remove,
  696. .set_online = &raw3215_set_online,
  697. .set_offline = &raw3215_set_offline,
  698. };
  699. #ifdef CONFIG_TN3215_CONSOLE
  700. /*
  701. * Write a string to the 3215 console
  702. */
  703. static void
  704. con3215_write(struct console *co, const char *str, unsigned int count)
  705. {
  706. struct raw3215_info *raw;
  707. int i;
  708. if (count <= 0)
  709. return;
  710. raw = raw3215[0]; /* console 3215 is the first one */
  711. while (count > 0) {
  712. for (i = 0; i < count; i++)
  713. if (str[i] == '\t' || str[i] == '\n')
  714. break;
  715. raw3215_write(raw, str, i);
  716. count -= i;
  717. str += i;
  718. if (count > 0) {
  719. raw3215_putchar(raw, *str);
  720. count--;
  721. str++;
  722. }
  723. }
  724. }
  725. static struct tty_driver *con3215_device(struct console *c, int *index)
  726. {
  727. *index = c->index;
  728. return tty3215_driver;
  729. }
  730. /*
  731. * panic() calls console_unblank before the system enters a
  732. * disabled, endless loop.
  733. */
  734. static void
  735. con3215_unblank(void)
  736. {
  737. struct raw3215_info *raw;
  738. unsigned long flags;
  739. raw = raw3215[0]; /* console 3215 is the first one */
  740. spin_lock_irqsave(raw->lock, flags);
  741. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  742. spin_unlock_irqrestore(raw->lock, flags);
  743. }
  744. static int __init
  745. con3215_consetup(struct console *co, char *options)
  746. {
  747. return 0;
  748. }
  749. /*
  750. * The console structure for the 3215 console
  751. */
  752. static struct console con3215 = {
  753. .name = "ttyS",
  754. .write = con3215_write,
  755. .device = con3215_device,
  756. .unblank = con3215_unblank,
  757. .setup = con3215_consetup,
  758. .flags = CON_PRINTBUFFER,
  759. };
  760. /*
  761. * 3215 console initialization code called from console_init().
  762. * NOTE: This is called before kmalloc is available.
  763. */
  764. static int __init
  765. con3215_init(void)
  766. {
  767. struct ccw_device *cdev;
  768. struct raw3215_info *raw;
  769. struct raw3215_req *req;
  770. int i;
  771. /* Check if 3215 is to be the console */
  772. if (!CONSOLE_IS_3215)
  773. return -ENODEV;
  774. /* Set the console mode for VM */
  775. if (MACHINE_IS_VM) {
  776. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  777. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  778. }
  779. /* allocate 3215 request structures */
  780. raw3215_freelist = NULL;
  781. spin_lock_init(&raw3215_freelist_lock);
  782. for (i = 0; i < NR_3215_REQ; i++) {
  783. req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
  784. req->next = raw3215_freelist;
  785. raw3215_freelist = req;
  786. }
  787. cdev = ccw_device_probe_console();
  788. if (IS_ERR(cdev))
  789. return -ENODEV;
  790. raw3215[0] = raw = (struct raw3215_info *)
  791. alloc_bootmem_low(sizeof(struct raw3215_info));
  792. memset(raw, 0, sizeof(struct raw3215_info));
  793. raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
  794. raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
  795. raw->cdev = cdev;
  796. raw->lock = get_ccwdev_lock(cdev);
  797. cdev->dev.driver_data = raw;
  798. cdev->handler = raw3215_irq;
  799. raw->flags |= RAW3215_FIXED;
  800. tasklet_init(&raw->tasklet,
  801. (void (*)(unsigned long)) raw3215_tasklet,
  802. (unsigned long) raw);
  803. init_waitqueue_head(&raw->empty_wait);
  804. /* Request the console irq */
  805. if (raw3215_startup(raw) != 0) {
  806. free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
  807. free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
  808. free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
  809. raw3215[0] = NULL;
  810. printk("Couldn't find a 3215 console device\n");
  811. return -ENODEV;
  812. }
  813. register_console(&con3215);
  814. return 0;
  815. }
  816. console_initcall(con3215_init);
  817. #endif
  818. /*
  819. * tty3215_open
  820. *
  821. * This routine is called whenever a 3215 tty is opened.
  822. */
  823. static int
  824. tty3215_open(struct tty_struct *tty, struct file * filp)
  825. {
  826. struct raw3215_info *raw;
  827. int retval, line;
  828. line = tty->index;
  829. if ((line < 0) || (line >= NR_3215))
  830. return -ENODEV;
  831. raw = raw3215[line];
  832. if (raw == NULL)
  833. return -ENODEV;
  834. tty->driver_data = raw;
  835. raw->tty = tty;
  836. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  837. /*
  838. * Start up 3215 device
  839. */
  840. retval = raw3215_startup(raw);
  841. if (retval)
  842. return retval;
  843. return 0;
  844. }
  845. /*
  846. * tty3215_close()
  847. *
  848. * This routine is called when the 3215 tty is closed. We wait
  849. * for the remaining request to be completed. Then we clean up.
  850. */
  851. static void
  852. tty3215_close(struct tty_struct *tty, struct file * filp)
  853. {
  854. struct raw3215_info *raw;
  855. raw = (struct raw3215_info *) tty->driver_data;
  856. if (raw == NULL || tty->count > 1)
  857. return;
  858. tty->closing = 1;
  859. /* Shutdown the terminal */
  860. raw3215_shutdown(raw);
  861. tty->closing = 0;
  862. raw->tty = NULL;
  863. }
  864. /*
  865. * Returns the amount of free space in the output buffer.
  866. */
  867. static int
  868. tty3215_write_room(struct tty_struct *tty)
  869. {
  870. struct raw3215_info *raw;
  871. raw = (struct raw3215_info *) tty->driver_data;
  872. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  873. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  874. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  875. else
  876. return 0;
  877. }
  878. /*
  879. * String write routine for 3215 ttys
  880. */
  881. static int
  882. tty3215_write(struct tty_struct * tty,
  883. const unsigned char *buf, int count)
  884. {
  885. struct raw3215_info *raw;
  886. if (!tty)
  887. return 0;
  888. raw = (struct raw3215_info *) tty->driver_data;
  889. raw3215_write(raw, buf, count);
  890. return count;
  891. }
  892. /*
  893. * Put character routine for 3215 ttys
  894. */
  895. static void
  896. tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  897. {
  898. struct raw3215_info *raw;
  899. if (!tty)
  900. return;
  901. raw = (struct raw3215_info *) tty->driver_data;
  902. raw3215_putchar(raw, ch);
  903. }
  904. static void
  905. tty3215_flush_chars(struct tty_struct *tty)
  906. {
  907. }
  908. /*
  909. * Returns the number of characters in the output buffer
  910. */
  911. static int
  912. tty3215_chars_in_buffer(struct tty_struct *tty)
  913. {
  914. struct raw3215_info *raw;
  915. raw = (struct raw3215_info *) tty->driver_data;
  916. return raw->count;
  917. }
  918. static void
  919. tty3215_flush_buffer(struct tty_struct *tty)
  920. {
  921. struct raw3215_info *raw;
  922. raw = (struct raw3215_info *) tty->driver_data;
  923. raw3215_flush_buffer(raw);
  924. tty_wakeup(tty);
  925. }
  926. /*
  927. * Currently we don't have any io controls for 3215 ttys
  928. */
  929. static int
  930. tty3215_ioctl(struct tty_struct *tty, struct file * file,
  931. unsigned int cmd, unsigned long arg)
  932. {
  933. if (tty->flags & (1 << TTY_IO_ERROR))
  934. return -EIO;
  935. switch (cmd) {
  936. default:
  937. return -ENOIOCTLCMD;
  938. }
  939. return 0;
  940. }
  941. /*
  942. * Disable reading from a 3215 tty
  943. */
  944. static void
  945. tty3215_throttle(struct tty_struct * tty)
  946. {
  947. struct raw3215_info *raw;
  948. raw = (struct raw3215_info *) tty->driver_data;
  949. raw->flags |= RAW3215_THROTTLED;
  950. }
  951. /*
  952. * Enable reading from a 3215 tty
  953. */
  954. static void
  955. tty3215_unthrottle(struct tty_struct * tty)
  956. {
  957. struct raw3215_info *raw;
  958. unsigned long flags;
  959. raw = (struct raw3215_info *) tty->driver_data;
  960. if (raw->flags & RAW3215_THROTTLED) {
  961. spin_lock_irqsave(raw->lock, flags);
  962. raw->flags &= ~RAW3215_THROTTLED;
  963. raw3215_try_io(raw);
  964. spin_unlock_irqrestore(raw->lock, flags);
  965. }
  966. }
  967. /*
  968. * Disable writing to a 3215 tty
  969. */
  970. static void
  971. tty3215_stop(struct tty_struct *tty)
  972. {
  973. struct raw3215_info *raw;
  974. raw = (struct raw3215_info *) tty->driver_data;
  975. raw->flags |= RAW3215_STOPPED;
  976. }
  977. /*
  978. * Enable writing to a 3215 tty
  979. */
  980. static void
  981. tty3215_start(struct tty_struct *tty)
  982. {
  983. struct raw3215_info *raw;
  984. unsigned long flags;
  985. raw = (struct raw3215_info *) tty->driver_data;
  986. if (raw->flags & RAW3215_STOPPED) {
  987. spin_lock_irqsave(raw->lock, flags);
  988. raw->flags &= ~RAW3215_STOPPED;
  989. raw3215_try_io(raw);
  990. spin_unlock_irqrestore(raw->lock, flags);
  991. }
  992. }
  993. static struct tty_operations tty3215_ops = {
  994. .open = tty3215_open,
  995. .close = tty3215_close,
  996. .write = tty3215_write,
  997. .put_char = tty3215_put_char,
  998. .flush_chars = tty3215_flush_chars,
  999. .write_room = tty3215_write_room,
  1000. .chars_in_buffer = tty3215_chars_in_buffer,
  1001. .flush_buffer = tty3215_flush_buffer,
  1002. .ioctl = tty3215_ioctl,
  1003. .throttle = tty3215_throttle,
  1004. .unthrottle = tty3215_unthrottle,
  1005. .stop = tty3215_stop,
  1006. .start = tty3215_start,
  1007. };
  1008. /*
  1009. * 3215 tty registration code called from tty_init().
  1010. * Most kernel services (incl. kmalloc) are available at this poimt.
  1011. */
  1012. int __init
  1013. tty3215_init(void)
  1014. {
  1015. struct tty_driver *driver;
  1016. int ret;
  1017. if (!CONSOLE_IS_3215)
  1018. return 0;
  1019. driver = alloc_tty_driver(NR_3215);
  1020. if (!driver)
  1021. return -ENOMEM;
  1022. ret = ccw_driver_register(&raw3215_ccw_driver);
  1023. if (ret) {
  1024. put_tty_driver(driver);
  1025. return ret;
  1026. }
  1027. /*
  1028. * Initialize the tty_driver structure
  1029. * Entries in tty3215_driver that are NOT initialized:
  1030. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1031. */
  1032. driver->owner = THIS_MODULE;
  1033. driver->driver_name = "tty3215";
  1034. driver->name = "ttyS";
  1035. driver->major = TTY_MAJOR;
  1036. driver->minor_start = 64;
  1037. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1038. driver->subtype = SYSTEM_TYPE_TTY;
  1039. driver->init_termios = tty_std_termios;
  1040. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1041. driver->init_termios.c_oflag = ONLCR | XTABS;
  1042. driver->init_termios.c_lflag = ISIG;
  1043. driver->flags = TTY_DRIVER_REAL_RAW;
  1044. tty_set_operations(driver, &tty3215_ops);
  1045. ret = tty_register_driver(driver);
  1046. if (ret) {
  1047. printk("Couldn't register tty3215 driver\n");
  1048. put_tty_driver(driver);
  1049. return ret;
  1050. }
  1051. tty3215_driver = driver;
  1052. return 0;
  1053. }
  1054. static void __exit
  1055. tty3215_exit(void)
  1056. {
  1057. tty_unregister_driver(tty3215_driver);
  1058. put_tty_driver(tty3215_driver);
  1059. ccw_driver_unregister(&raw3215_ccw_driver);
  1060. }
  1061. module_init(tty3215_init);
  1062. module_exit(tty3215_exit);