con3215.c 30 KB

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