con3215.c 30 KB

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