con3215.c 29 KB

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