raw3270.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * drivers/s390/char/raw3270.c
  3. * IBM/3270 Driver - core functions.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  9. */
  10. #include <linux/bootmem.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/wait.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/cio.h>
  21. #include <asm/ebcdic.h>
  22. #include "raw3270.h"
  23. #include <linux/major.h>
  24. #include <linux/kdev_t.h>
  25. #include <linux/device.h>
  26. #include <linux/mutex.h>
  27. static struct class *class3270;
  28. /* The main 3270 data structure. */
  29. struct raw3270 {
  30. struct list_head list;
  31. struct ccw_device *cdev;
  32. int minor;
  33. short model, rows, cols;
  34. unsigned long flags;
  35. struct list_head req_queue; /* Request queue. */
  36. struct list_head view_list; /* List of available views. */
  37. struct raw3270_view *view; /* Active view. */
  38. struct timer_list timer; /* Device timer. */
  39. unsigned char *ascebc; /* ascii -> ebcdic table */
  40. struct class_device *clttydev; /* 3270-class tty device ptr */
  41. struct class_device *cltubdev; /* 3270-class tub device ptr */
  42. struct raw3270_request init_request;
  43. unsigned char init_data[256];
  44. };
  45. /* raw3270->flags */
  46. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  47. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  48. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  49. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  50. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  51. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  52. static DEFINE_MUTEX(raw3270_mutex);
  53. /* List of 3270 devices. */
  54. static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
  55. /*
  56. * Flag to indicate if the driver has been registered. Some operations
  57. * like waiting for the end of i/o need to be done differently as long
  58. * as the kernel is still starting up (console support).
  59. */
  60. static int raw3270_registered;
  61. /* Module parameters */
  62. static int tubxcorrect = 0;
  63. module_param(tubxcorrect, bool, 0);
  64. /*
  65. * Wait queue for device init/delete, view delete.
  66. */
  67. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  68. /*
  69. * Encode array for 12 bit 3270 addresses.
  70. */
  71. static unsigned char raw3270_ebcgraf[64] = {
  72. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  73. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  74. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  75. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  76. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  77. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  78. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  79. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  80. };
  81. void
  82. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  83. {
  84. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  85. cp[0] = (addr >> 8) & 0x3f;
  86. cp[1] = addr & 0xff;
  87. } else {
  88. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  89. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  90. }
  91. }
  92. /*
  93. * Allocate a new 3270 ccw request
  94. */
  95. struct raw3270_request *
  96. raw3270_request_alloc(size_t size)
  97. {
  98. struct raw3270_request *rq;
  99. /* Allocate request structure */
  100. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  101. if (!rq)
  102. return ERR_PTR(-ENOMEM);
  103. /* alloc output buffer. */
  104. if (size > 0) {
  105. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  106. if (!rq->buffer) {
  107. kfree(rq);
  108. return ERR_PTR(-ENOMEM);
  109. }
  110. }
  111. rq->size = size;
  112. INIT_LIST_HEAD(&rq->list);
  113. /*
  114. * Setup ccw.
  115. */
  116. rq->ccw.cda = __pa(rq->buffer);
  117. rq->ccw.flags = CCW_FLAG_SLI;
  118. return rq;
  119. }
  120. #ifdef CONFIG_TN3270_CONSOLE
  121. /*
  122. * Allocate a new 3270 ccw request from bootmem. Only works very
  123. * early in the boot process. Only con3270.c should be using this.
  124. */
  125. struct raw3270_request *
  126. raw3270_request_alloc_bootmem(size_t size)
  127. {
  128. struct raw3270_request *rq;
  129. rq = alloc_bootmem_low(sizeof(struct raw3270));
  130. if (!rq)
  131. return ERR_PTR(-ENOMEM);
  132. memset(rq, 0, sizeof(struct raw3270_request));
  133. /* alloc output buffer. */
  134. if (size > 0) {
  135. rq->buffer = alloc_bootmem_low(size);
  136. if (!rq->buffer) {
  137. free_bootmem((unsigned long) rq,
  138. sizeof(struct raw3270));
  139. return ERR_PTR(-ENOMEM);
  140. }
  141. }
  142. rq->size = size;
  143. INIT_LIST_HEAD(&rq->list);
  144. /*
  145. * Setup ccw.
  146. */
  147. rq->ccw.cda = __pa(rq->buffer);
  148. rq->ccw.flags = CCW_FLAG_SLI;
  149. return rq;
  150. }
  151. #endif
  152. /*
  153. * Free 3270 ccw request
  154. */
  155. void
  156. raw3270_request_free (struct raw3270_request *rq)
  157. {
  158. kfree(rq->buffer);
  159. kfree(rq);
  160. }
  161. /*
  162. * Reset request to initial state.
  163. */
  164. void
  165. raw3270_request_reset(struct raw3270_request *rq)
  166. {
  167. BUG_ON(!list_empty(&rq->list));
  168. rq->ccw.cmd_code = 0;
  169. rq->ccw.count = 0;
  170. rq->ccw.cda = __pa(rq->buffer);
  171. rq->ccw.flags = CCW_FLAG_SLI;
  172. rq->rescnt = 0;
  173. rq->rc = 0;
  174. }
  175. /*
  176. * Set command code to ccw of a request.
  177. */
  178. void
  179. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  180. {
  181. rq->ccw.cmd_code = cmd;
  182. }
  183. /*
  184. * Add data fragment to output buffer.
  185. */
  186. int
  187. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  188. {
  189. if (size + rq->ccw.count > rq->size)
  190. return -E2BIG;
  191. memcpy(rq->buffer + rq->ccw.count, data, size);
  192. rq->ccw.count += size;
  193. return 0;
  194. }
  195. /*
  196. * Set address/length pair to ccw of a request.
  197. */
  198. void
  199. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  200. {
  201. rq->ccw.cda = __pa(data);
  202. rq->ccw.count = size;
  203. }
  204. /*
  205. * Set idal buffer to ccw of a request.
  206. */
  207. void
  208. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  209. {
  210. rq->ccw.cda = __pa(ib->data);
  211. rq->ccw.count = ib->size;
  212. rq->ccw.flags |= CCW_FLAG_IDA;
  213. }
  214. /*
  215. * Stop running ccw.
  216. */
  217. static int
  218. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  219. {
  220. int retries;
  221. int rc;
  222. if (raw3270_request_final(rq))
  223. return 0;
  224. /* Check if interrupt has already been processed */
  225. for (retries = 0; retries < 5; retries++) {
  226. if (retries < 2)
  227. rc = ccw_device_halt(rp->cdev, (long) rq);
  228. else
  229. rc = ccw_device_clear(rp->cdev, (long) rq);
  230. if (rc == 0)
  231. break; /* termination successful */
  232. }
  233. return rc;
  234. }
  235. static int
  236. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  237. {
  238. unsigned long flags;
  239. int rc;
  240. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  241. rc = raw3270_halt_io_nolock(rp, rq);
  242. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  243. return rc;
  244. }
  245. /*
  246. * Add the request to the request queue, try to start it if the
  247. * 3270 device is idle. Return without waiting for end of i/o.
  248. */
  249. static int
  250. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  251. struct raw3270_request *rq)
  252. {
  253. rq->view = view;
  254. raw3270_get_view(view);
  255. if (list_empty(&rp->req_queue) &&
  256. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  257. /* No other requests are on the queue. Start this one. */
  258. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  259. (unsigned long) rq, 0, 0);
  260. if (rq->rc) {
  261. raw3270_put_view(view);
  262. return rq->rc;
  263. }
  264. }
  265. list_add_tail(&rq->list, &rp->req_queue);
  266. return 0;
  267. }
  268. int
  269. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  270. {
  271. unsigned long flags;
  272. struct raw3270 *rp;
  273. int rc;
  274. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  275. rp = view->dev;
  276. if (!rp || rp->view != view)
  277. rc = -EACCES;
  278. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  279. rc = -ENODEV;
  280. else
  281. rc = __raw3270_start(rp, view, rq);
  282. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  283. return rc;
  284. }
  285. int
  286. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  287. {
  288. struct raw3270 *rp;
  289. int rc;
  290. rp = view->dev;
  291. if (!rp || rp->view != view)
  292. rc = -EACCES;
  293. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  294. rc = -ENODEV;
  295. else
  296. rc = __raw3270_start(rp, view, rq);
  297. return rc;
  298. }
  299. int
  300. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  301. {
  302. struct raw3270 *rp;
  303. rp = view->dev;
  304. rq->view = view;
  305. raw3270_get_view(view);
  306. list_add_tail(&rq->list, &rp->req_queue);
  307. return 0;
  308. }
  309. /*
  310. * 3270 interrupt routine, called from the ccw_device layer
  311. */
  312. static void
  313. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  314. {
  315. struct raw3270 *rp;
  316. struct raw3270_view *view;
  317. struct raw3270_request *rq;
  318. int rc;
  319. rp = (struct raw3270 *) cdev->dev.driver_data;
  320. if (!rp)
  321. return;
  322. rq = (struct raw3270_request *) intparm;
  323. view = rq ? rq->view : rp->view;
  324. if (IS_ERR(irb))
  325. rc = RAW3270_IO_RETRY;
  326. else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
  327. rq->rc = -EIO;
  328. rc = RAW3270_IO_DONE;
  329. } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  330. DEV_STAT_UNIT_EXCEP)) {
  331. /* Handle CE-DE-UE and subsequent UDE */
  332. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  333. rc = RAW3270_IO_BUSY;
  334. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  335. /* Wait for UDE if busy flag is set. */
  336. if (irb->scsw.dstat & DEV_STAT_DEV_END) {
  337. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  338. /* Got it, now retry. */
  339. rc = RAW3270_IO_RETRY;
  340. } else
  341. rc = RAW3270_IO_BUSY;
  342. } else if (view)
  343. rc = view->fn->intv(view, rq, irb);
  344. else
  345. rc = RAW3270_IO_DONE;
  346. switch (rc) {
  347. case RAW3270_IO_DONE:
  348. break;
  349. case RAW3270_IO_BUSY:
  350. /*
  351. * Intervention required by the operator. We have to wait
  352. * for unsolicited device end.
  353. */
  354. return;
  355. case RAW3270_IO_RETRY:
  356. if (!rq)
  357. break;
  358. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  359. (unsigned long) rq, 0, 0);
  360. if (rq->rc == 0)
  361. return; /* Sucessfully restarted. */
  362. break;
  363. case RAW3270_IO_STOP:
  364. if (!rq)
  365. break;
  366. raw3270_halt_io_nolock(rp, rq);
  367. rq->rc = -EIO;
  368. break;
  369. default:
  370. BUG();
  371. }
  372. if (rq) {
  373. BUG_ON(list_empty(&rq->list));
  374. /* The request completed, remove from queue and do callback. */
  375. list_del_init(&rq->list);
  376. if (rq->callback)
  377. rq->callback(rq, rq->callback_data);
  378. /* Do put_device for get_device in raw3270_start. */
  379. raw3270_put_view(view);
  380. }
  381. /*
  382. * Try to start each request on request queue until one is
  383. * started successful.
  384. */
  385. while (!list_empty(&rp->req_queue)) {
  386. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  387. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  388. (unsigned long) rq, 0, 0);
  389. if (rq->rc == 0)
  390. break;
  391. /* Start failed. Remove request and do callback. */
  392. list_del_init(&rq->list);
  393. if (rq->callback)
  394. rq->callback(rq, rq->callback_data);
  395. /* Do put_device for get_device in raw3270_start. */
  396. raw3270_put_view(view);
  397. }
  398. }
  399. /*
  400. * Size sensing.
  401. */
  402. struct raw3270_ua { /* Query Reply structure for Usable Area */
  403. struct { /* Usable Area Query Reply Base */
  404. short l; /* Length of this structured field */
  405. char sfid; /* 0x81 if Query Reply */
  406. char qcode; /* 0x81 if Usable Area */
  407. char flags0;
  408. char flags1;
  409. short w; /* Width of usable area */
  410. short h; /* Heigth of usavle area */
  411. char units; /* 0x00:in; 0x01:mm */
  412. int xr;
  413. int yr;
  414. char aw;
  415. char ah;
  416. short buffsz; /* Character buffer size, bytes */
  417. char xmin;
  418. char ymin;
  419. char xmax;
  420. char ymax;
  421. } __attribute__ ((packed)) uab;
  422. struct { /* Alternate Usable Area Self-Defining Parameter */
  423. char l; /* Length of this Self-Defining Parm */
  424. char sdpid; /* 0x02 if Alternate Usable Area */
  425. char res;
  426. char auaid; /* 0x01 is Id for the A U A */
  427. short wauai; /* Width of AUAi */
  428. short hauai; /* Height of AUAi */
  429. char auaunits; /* 0x00:in, 0x01:mm */
  430. int auaxr;
  431. int auayr;
  432. char awauai;
  433. char ahauai;
  434. } __attribute__ ((packed)) aua;
  435. } __attribute__ ((packed));
  436. static struct diag210 raw3270_init_diag210;
  437. static DECLARE_MUTEX(raw3270_init_sem);
  438. static int
  439. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  440. struct irb *irb)
  441. {
  442. /*
  443. * Unit-Check Processing:
  444. * Expect Command Reject or Intervention Required.
  445. */
  446. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  447. /* Request finished abnormally. */
  448. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  449. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  450. return RAW3270_IO_BUSY;
  451. }
  452. }
  453. if (rq) {
  454. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  455. if (irb->ecw[0] & SNS0_CMD_REJECT)
  456. rq->rc = -EOPNOTSUPP;
  457. else
  458. rq->rc = -EIO;
  459. } else
  460. /* Request finished normally. Copy residual count. */
  461. rq->rescnt = irb->scsw.count;
  462. }
  463. if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
  464. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  465. wake_up(&raw3270_wait_queue);
  466. }
  467. return RAW3270_IO_DONE;
  468. }
  469. static struct raw3270_fn raw3270_init_fn = {
  470. .intv = raw3270_init_irq
  471. };
  472. static struct raw3270_view raw3270_init_view = {
  473. .fn = &raw3270_init_fn
  474. };
  475. /*
  476. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  477. * Wait for end of request. The request must have been started
  478. * with raw3270_start, rc = 0. The device lock may NOT have been
  479. * released between calling raw3270_start and raw3270_wait.
  480. */
  481. static void
  482. raw3270_wake_init(struct raw3270_request *rq, void *data)
  483. {
  484. wake_up((wait_queue_head_t *) data);
  485. }
  486. /*
  487. * Special wait function that can cope with console initialization.
  488. */
  489. static int
  490. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  491. struct raw3270_request *rq)
  492. {
  493. unsigned long flags;
  494. wait_queue_head_t wq;
  495. int rc;
  496. #ifdef CONFIG_TN3270_CONSOLE
  497. if (raw3270_registered == 0) {
  498. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  499. rq->callback = NULL;
  500. rc = __raw3270_start(rp, view, rq);
  501. if (rc == 0)
  502. while (!raw3270_request_final(rq)) {
  503. wait_cons_dev();
  504. barrier();
  505. }
  506. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  507. return rq->rc;
  508. }
  509. #endif
  510. init_waitqueue_head(&wq);
  511. rq->callback = raw3270_wake_init;
  512. rq->callback_data = &wq;
  513. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  514. rc = __raw3270_start(rp, view, rq);
  515. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  516. if (rc)
  517. return rc;
  518. /* Now wait for the completion. */
  519. rc = wait_event_interruptible(wq, raw3270_request_final(rq));
  520. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  521. raw3270_halt_io(view->dev, rq);
  522. /* No wait for the halt to complete. */
  523. wait_event(wq, raw3270_request_final(rq));
  524. return -ERESTARTSYS;
  525. }
  526. return rq->rc;
  527. }
  528. static int
  529. __raw3270_size_device_vm(struct raw3270 *rp)
  530. {
  531. int rc, model;
  532. struct ccw_dev_id dev_id;
  533. ccw_device_get_id(rp->cdev, &dev_id);
  534. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  535. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  536. rc = diag210(&raw3270_init_diag210);
  537. if (rc)
  538. return rc;
  539. model = raw3270_init_diag210.vrdccrmd;
  540. switch (model) {
  541. case 2:
  542. rp->model = model;
  543. rp->rows = 24;
  544. rp->cols = 80;
  545. break;
  546. case 3:
  547. rp->model = model;
  548. rp->rows = 32;
  549. rp->cols = 80;
  550. break;
  551. case 4:
  552. rp->model = model;
  553. rp->rows = 43;
  554. rp->cols = 80;
  555. break;
  556. case 5:
  557. rp->model = model;
  558. rp->rows = 27;
  559. rp->cols = 132;
  560. break;
  561. default:
  562. printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
  563. rc = -EOPNOTSUPP;
  564. break;
  565. }
  566. return rc;
  567. }
  568. static int
  569. __raw3270_size_device(struct raw3270 *rp)
  570. {
  571. static const unsigned char wbuf[] =
  572. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  573. struct raw3270_ua *uap;
  574. unsigned short count;
  575. int rc;
  576. /*
  577. * To determine the size of the 3270 device we need to do:
  578. * 1) send a 'read partition' data stream to the device
  579. * 2) wait for the attn interrupt that preceeds the query reply
  580. * 3) do a read modified to get the query reply
  581. * To make things worse we have to cope with intervention
  582. * required (3270 device switched to 'stand-by') and command
  583. * rejects (old devices that can't do 'read partition').
  584. */
  585. memset(&rp->init_request, 0, sizeof(rp->init_request));
  586. memset(&rp->init_data, 0, 256);
  587. /* Store 'read partition' data stream to init_data */
  588. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  589. INIT_LIST_HEAD(&rp->init_request.list);
  590. rp->init_request.ccw.cmd_code = TC_WRITESF;
  591. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  592. rp->init_request.ccw.count = sizeof(wbuf);
  593. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  594. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  595. if (rc)
  596. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  597. return rc;
  598. /* Wait for attention interrupt. */
  599. #ifdef CONFIG_TN3270_CONSOLE
  600. if (raw3270_registered == 0) {
  601. unsigned long flags;
  602. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  603. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  604. wait_cons_dev();
  605. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  606. } else
  607. #endif
  608. rc = wait_event_interruptible(raw3270_wait_queue,
  609. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  610. if (rc)
  611. return rc;
  612. /*
  613. * The device accepted the 'read partition' command. Now
  614. * set up a read ccw and issue it.
  615. */
  616. rp->init_request.ccw.cmd_code = TC_READMOD;
  617. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  618. rp->init_request.ccw.count = sizeof(rp->init_data);
  619. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  620. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  621. if (rc)
  622. return rc;
  623. /* Got a Query Reply */
  624. count = sizeof(rp->init_data) - rp->init_request.rescnt;
  625. uap = (struct raw3270_ua *) (rp->init_data + 1);
  626. /* Paranoia check. */
  627. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  628. return -EOPNOTSUPP;
  629. /* Copy rows/columns of default Usable Area */
  630. rp->rows = uap->uab.h;
  631. rp->cols = uap->uab.w;
  632. /* Check for 14 bit addressing */
  633. if ((uap->uab.flags0 & 0x0d) == 0x01)
  634. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  635. /* Check for Alternate Usable Area */
  636. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  637. uap->aua.sdpid == 0x02) {
  638. rp->rows = uap->aua.hauai;
  639. rp->cols = uap->aua.wauai;
  640. }
  641. return 0;
  642. }
  643. static int
  644. raw3270_size_device(struct raw3270 *rp)
  645. {
  646. int rc;
  647. down(&raw3270_init_sem);
  648. rp->view = &raw3270_init_view;
  649. raw3270_init_view.dev = rp;
  650. if (MACHINE_IS_VM)
  651. rc = __raw3270_size_device_vm(rp);
  652. else
  653. rc = __raw3270_size_device(rp);
  654. raw3270_init_view.dev = NULL;
  655. rp->view = NULL;
  656. up(&raw3270_init_sem);
  657. if (rc == 0) { /* Found something. */
  658. /* Try to find a model. */
  659. rp->model = 0;
  660. if (rp->rows == 24 && rp->cols == 80)
  661. rp->model = 2;
  662. if (rp->rows == 32 && rp->cols == 80)
  663. rp->model = 3;
  664. if (rp->rows == 43 && rp->cols == 80)
  665. rp->model = 4;
  666. if (rp->rows == 27 && rp->cols == 132)
  667. rp->model = 5;
  668. } else {
  669. /* Couldn't detect size. Use default model 2. */
  670. rp->model = 2;
  671. rp->rows = 24;
  672. rp->cols = 80;
  673. return 0;
  674. }
  675. return rc;
  676. }
  677. static int
  678. raw3270_reset_device(struct raw3270 *rp)
  679. {
  680. int rc;
  681. down(&raw3270_init_sem);
  682. memset(&rp->init_request, 0, sizeof(rp->init_request));
  683. memset(&rp->init_data, 0, sizeof(rp->init_data));
  684. /* Store reset data stream to init_data/init_request */
  685. rp->init_data[0] = TW_KR;
  686. INIT_LIST_HEAD(&rp->init_request.list);
  687. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  688. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  689. rp->init_request.ccw.count = 1;
  690. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  691. rp->view = &raw3270_init_view;
  692. raw3270_init_view.dev = rp;
  693. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  694. raw3270_init_view.dev = NULL;
  695. rp->view = NULL;
  696. up(&raw3270_init_sem);
  697. return rc;
  698. }
  699. int
  700. raw3270_reset(struct raw3270_view *view)
  701. {
  702. struct raw3270 *rp;
  703. int rc;
  704. rp = view->dev;
  705. if (!rp || rp->view != view)
  706. rc = -EACCES;
  707. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  708. rc = -ENODEV;
  709. else
  710. rc = raw3270_reset_device(view->dev);
  711. return rc;
  712. }
  713. /*
  714. * Setup new 3270 device.
  715. */
  716. static int
  717. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  718. {
  719. struct list_head *l;
  720. struct raw3270 *tmp;
  721. int minor;
  722. memset(rp, 0, sizeof(struct raw3270));
  723. /* Copy ebcdic -> ascii translation table. */
  724. memcpy(ascebc, _ascebc, 256);
  725. if (tubxcorrect) {
  726. /* correct brackets and circumflex */
  727. ascebc['['] = 0xad;
  728. ascebc[']'] = 0xbd;
  729. ascebc['^'] = 0xb0;
  730. }
  731. rp->ascebc = ascebc;
  732. /* Set defaults. */
  733. rp->rows = 24;
  734. rp->cols = 80;
  735. INIT_LIST_HEAD(&rp->req_queue);
  736. INIT_LIST_HEAD(&rp->view_list);
  737. /*
  738. * Add device to list and find the smallest unused minor
  739. * number for it. Note: there is no device with minor 0,
  740. * see special case for fs3270.c:fs3270_open().
  741. */
  742. mutex_lock(&raw3270_mutex);
  743. /* Keep the list sorted. */
  744. minor = RAW3270_FIRSTMINOR;
  745. rp->minor = -1;
  746. list_for_each(l, &raw3270_devices) {
  747. tmp = list_entry(l, struct raw3270, list);
  748. if (tmp->minor > minor) {
  749. rp->minor = minor;
  750. __list_add(&rp->list, l->prev, l);
  751. break;
  752. }
  753. minor++;
  754. }
  755. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  756. rp->minor = minor;
  757. list_add_tail(&rp->list, &raw3270_devices);
  758. }
  759. mutex_unlock(&raw3270_mutex);
  760. /* No free minor number? Then give up. */
  761. if (rp->minor == -1)
  762. return -EUSERS;
  763. rp->cdev = cdev;
  764. cdev->dev.driver_data = rp;
  765. cdev->handler = raw3270_irq;
  766. return 0;
  767. }
  768. #ifdef CONFIG_TN3270_CONSOLE
  769. /*
  770. * Setup 3270 device configured as console.
  771. */
  772. struct raw3270 *
  773. raw3270_setup_console(struct ccw_device *cdev)
  774. {
  775. struct raw3270 *rp;
  776. char *ascebc;
  777. int rc;
  778. rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
  779. ascebc = (char *) alloc_bootmem(256);
  780. rc = raw3270_setup_device(cdev, rp, ascebc);
  781. if (rc)
  782. return ERR_PTR(rc);
  783. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  784. rc = raw3270_reset_device(rp);
  785. if (rc)
  786. return ERR_PTR(rc);
  787. rc = raw3270_size_device(rp);
  788. if (rc)
  789. return ERR_PTR(rc);
  790. rc = raw3270_reset_device(rp);
  791. if (rc)
  792. return ERR_PTR(rc);
  793. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  794. return rp;
  795. }
  796. void
  797. raw3270_wait_cons_dev(struct raw3270 *rp)
  798. {
  799. unsigned long flags;
  800. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  801. wait_cons_dev();
  802. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  803. }
  804. #endif
  805. /*
  806. * Create a 3270 device structure.
  807. */
  808. static struct raw3270 *
  809. raw3270_create_device(struct ccw_device *cdev)
  810. {
  811. struct raw3270 *rp;
  812. char *ascebc;
  813. int rc;
  814. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  815. if (!rp)
  816. return ERR_PTR(-ENOMEM);
  817. ascebc = kmalloc(256, GFP_KERNEL);
  818. if (!ascebc) {
  819. kfree(rp);
  820. return ERR_PTR(-ENOMEM);
  821. }
  822. rc = raw3270_setup_device(cdev, rp, ascebc);
  823. if (rc) {
  824. kfree(rp->ascebc);
  825. kfree(rp);
  826. rp = ERR_PTR(rc);
  827. }
  828. /* Get reference to ccw_device structure. */
  829. get_device(&cdev->dev);
  830. return rp;
  831. }
  832. /*
  833. * Activate a view.
  834. */
  835. int
  836. raw3270_activate_view(struct raw3270_view *view)
  837. {
  838. struct raw3270 *rp;
  839. struct raw3270_view *oldview, *nv;
  840. unsigned long flags;
  841. int rc;
  842. rp = view->dev;
  843. if (!rp)
  844. return -ENODEV;
  845. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  846. if (rp->view == view)
  847. rc = 0;
  848. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  849. rc = -ENODEV;
  850. else {
  851. oldview = NULL;
  852. if (rp->view) {
  853. oldview = rp->view;
  854. oldview->fn->deactivate(oldview);
  855. }
  856. rp->view = view;
  857. rc = view->fn->activate(view);
  858. if (rc) {
  859. /* Didn't work. Try to reactivate the old view. */
  860. rp->view = oldview;
  861. if (!oldview || oldview->fn->activate(oldview) != 0) {
  862. /* Didn't work as well. Try any other view. */
  863. list_for_each_entry(nv, &rp->view_list, list)
  864. if (nv != view && nv != oldview) {
  865. rp->view = nv;
  866. if (nv->fn->activate(nv) == 0)
  867. break;
  868. rp->view = NULL;
  869. }
  870. }
  871. }
  872. }
  873. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  874. return rc;
  875. }
  876. /*
  877. * Deactivate current view.
  878. */
  879. void
  880. raw3270_deactivate_view(struct raw3270_view *view)
  881. {
  882. unsigned long flags;
  883. struct raw3270 *rp;
  884. rp = view->dev;
  885. if (!rp)
  886. return;
  887. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  888. if (rp->view == view) {
  889. view->fn->deactivate(view);
  890. rp->view = NULL;
  891. /* Move deactivated view to end of list. */
  892. list_del_init(&view->list);
  893. list_add_tail(&view->list, &rp->view_list);
  894. /* Try to activate another view. */
  895. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  896. list_for_each_entry(view, &rp->view_list, list) {
  897. rp->view = view;
  898. if (view->fn->activate(view) == 0)
  899. break;
  900. rp->view = NULL;
  901. }
  902. }
  903. }
  904. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  905. }
  906. /*
  907. * Add view to device with minor "minor".
  908. */
  909. int
  910. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  911. {
  912. unsigned long flags;
  913. struct raw3270 *rp;
  914. int rc;
  915. if (minor <= 0)
  916. return -ENODEV;
  917. mutex_lock(&raw3270_mutex);
  918. rc = -ENODEV;
  919. list_for_each_entry(rp, &raw3270_devices, list) {
  920. if (rp->minor != minor)
  921. continue;
  922. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  923. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  924. atomic_set(&view->ref_count, 2);
  925. view->dev = rp;
  926. view->fn = fn;
  927. view->model = rp->model;
  928. view->rows = rp->rows;
  929. view->cols = rp->cols;
  930. view->ascebc = rp->ascebc;
  931. spin_lock_init(&view->lock);
  932. list_add(&view->list, &rp->view_list);
  933. rc = 0;
  934. }
  935. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  936. break;
  937. }
  938. mutex_unlock(&raw3270_mutex);
  939. return rc;
  940. }
  941. /*
  942. * Find specific view of device with minor "minor".
  943. */
  944. struct raw3270_view *
  945. raw3270_find_view(struct raw3270_fn *fn, int minor)
  946. {
  947. struct raw3270 *rp;
  948. struct raw3270_view *view, *tmp;
  949. unsigned long flags;
  950. mutex_lock(&raw3270_mutex);
  951. view = ERR_PTR(-ENODEV);
  952. list_for_each_entry(rp, &raw3270_devices, list) {
  953. if (rp->minor != minor)
  954. continue;
  955. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  956. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  957. view = ERR_PTR(-ENOENT);
  958. list_for_each_entry(tmp, &rp->view_list, list) {
  959. if (tmp->fn == fn) {
  960. raw3270_get_view(tmp);
  961. view = tmp;
  962. break;
  963. }
  964. }
  965. }
  966. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  967. break;
  968. }
  969. mutex_unlock(&raw3270_mutex);
  970. return view;
  971. }
  972. /*
  973. * Remove view from device and free view structure via call to view->fn->free.
  974. */
  975. void
  976. raw3270_del_view(struct raw3270_view *view)
  977. {
  978. unsigned long flags;
  979. struct raw3270 *rp;
  980. struct raw3270_view *nv;
  981. rp = view->dev;
  982. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  983. if (rp->view == view) {
  984. view->fn->deactivate(view);
  985. rp->view = NULL;
  986. }
  987. list_del_init(&view->list);
  988. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  989. /* Try to activate another view. */
  990. list_for_each_entry(nv, &rp->view_list, list) {
  991. if (nv->fn->activate(nv) == 0) {
  992. rp->view = nv;
  993. break;
  994. }
  995. }
  996. }
  997. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  998. /* Wait for reference counter to drop to zero. */
  999. atomic_dec(&view->ref_count);
  1000. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  1001. if (view->fn->free)
  1002. view->fn->free(view);
  1003. }
  1004. /*
  1005. * Remove a 3270 device structure.
  1006. */
  1007. static void
  1008. raw3270_delete_device(struct raw3270 *rp)
  1009. {
  1010. struct ccw_device *cdev;
  1011. /* Remove from device chain. */
  1012. mutex_lock(&raw3270_mutex);
  1013. if (rp->clttydev && !IS_ERR(rp->clttydev))
  1014. class_device_destroy(class3270,
  1015. MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1016. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  1017. class_device_destroy(class3270,
  1018. MKDEV(IBM_FS3270_MAJOR, rp->minor));
  1019. list_del_init(&rp->list);
  1020. mutex_unlock(&raw3270_mutex);
  1021. /* Disconnect from ccw_device. */
  1022. cdev = rp->cdev;
  1023. rp->cdev = NULL;
  1024. cdev->dev.driver_data = NULL;
  1025. cdev->handler = NULL;
  1026. /* Put ccw_device structure. */
  1027. put_device(&cdev->dev);
  1028. /* Now free raw3270 structure. */
  1029. kfree(rp->ascebc);
  1030. kfree(rp);
  1031. }
  1032. static int
  1033. raw3270_probe (struct ccw_device *cdev)
  1034. {
  1035. return 0;
  1036. }
  1037. /*
  1038. * Additional attributes for a 3270 device
  1039. */
  1040. static ssize_t
  1041. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1042. {
  1043. return snprintf(buf, PAGE_SIZE, "%i\n",
  1044. ((struct raw3270 *) dev->driver_data)->model);
  1045. }
  1046. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1047. static ssize_t
  1048. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1049. {
  1050. return snprintf(buf, PAGE_SIZE, "%i\n",
  1051. ((struct raw3270 *) dev->driver_data)->rows);
  1052. }
  1053. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1054. static ssize_t
  1055. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1056. {
  1057. return snprintf(buf, PAGE_SIZE, "%i\n",
  1058. ((struct raw3270 *) dev->driver_data)->cols);
  1059. }
  1060. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1061. static struct attribute * raw3270_attrs[] = {
  1062. &dev_attr_model.attr,
  1063. &dev_attr_rows.attr,
  1064. &dev_attr_columns.attr,
  1065. NULL,
  1066. };
  1067. static struct attribute_group raw3270_attr_group = {
  1068. .attrs = raw3270_attrs,
  1069. };
  1070. static int raw3270_create_attributes(struct raw3270 *rp)
  1071. {
  1072. int rc;
  1073. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1074. if (rc)
  1075. goto out;
  1076. rp->clttydev = class_device_create(class3270, NULL,
  1077. MKDEV(IBM_TTY3270_MAJOR, rp->minor),
  1078. &rp->cdev->dev, "tty%s",
  1079. rp->cdev->dev.bus_id);
  1080. if (IS_ERR(rp->clttydev)) {
  1081. rc = PTR_ERR(rp->clttydev);
  1082. goto out_ttydev;
  1083. }
  1084. rp->cltubdev = class_device_create(class3270, NULL,
  1085. MKDEV(IBM_FS3270_MAJOR, rp->minor),
  1086. &rp->cdev->dev, "tub%s",
  1087. rp->cdev->dev.bus_id);
  1088. if (!IS_ERR(rp->cltubdev))
  1089. goto out;
  1090. rc = PTR_ERR(rp->cltubdev);
  1091. class_device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1092. out_ttydev:
  1093. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1094. out:
  1095. return rc;
  1096. }
  1097. /*
  1098. * Notifier for device addition/removal
  1099. */
  1100. struct raw3270_notifier {
  1101. struct list_head list;
  1102. void (*notifier)(int, int);
  1103. };
  1104. static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
  1105. int raw3270_register_notifier(void (*notifier)(int, int))
  1106. {
  1107. struct raw3270_notifier *np;
  1108. struct raw3270 *rp;
  1109. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1110. if (!np)
  1111. return -ENOMEM;
  1112. np->notifier = notifier;
  1113. mutex_lock(&raw3270_mutex);
  1114. list_add_tail(&np->list, &raw3270_notifier);
  1115. list_for_each_entry(rp, &raw3270_devices, list) {
  1116. get_device(&rp->cdev->dev);
  1117. notifier(rp->minor, 1);
  1118. }
  1119. mutex_unlock(&raw3270_mutex);
  1120. return 0;
  1121. }
  1122. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1123. {
  1124. struct raw3270_notifier *np;
  1125. mutex_lock(&raw3270_mutex);
  1126. list_for_each_entry(np, &raw3270_notifier, list)
  1127. if (np->notifier == notifier) {
  1128. list_del(&np->list);
  1129. kfree(np);
  1130. break;
  1131. }
  1132. mutex_unlock(&raw3270_mutex);
  1133. }
  1134. /*
  1135. * Set 3270 device online.
  1136. */
  1137. static int
  1138. raw3270_set_online (struct ccw_device *cdev)
  1139. {
  1140. struct raw3270 *rp;
  1141. struct raw3270_notifier *np;
  1142. int rc;
  1143. rp = raw3270_create_device(cdev);
  1144. if (IS_ERR(rp))
  1145. return PTR_ERR(rp);
  1146. rc = raw3270_reset_device(rp);
  1147. if (rc)
  1148. goto failure;
  1149. rc = raw3270_size_device(rp);
  1150. if (rc)
  1151. goto failure;
  1152. rc = raw3270_reset_device(rp);
  1153. if (rc)
  1154. goto failure;
  1155. rc = raw3270_create_attributes(rp);
  1156. if (rc)
  1157. goto failure;
  1158. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1159. mutex_lock(&raw3270_mutex);
  1160. list_for_each_entry(np, &raw3270_notifier, list)
  1161. np->notifier(rp->minor, 1);
  1162. mutex_unlock(&raw3270_mutex);
  1163. return 0;
  1164. failure:
  1165. raw3270_delete_device(rp);
  1166. return rc;
  1167. }
  1168. /*
  1169. * Remove 3270 device structure.
  1170. */
  1171. static void
  1172. raw3270_remove (struct ccw_device *cdev)
  1173. {
  1174. unsigned long flags;
  1175. struct raw3270 *rp;
  1176. struct raw3270_view *v;
  1177. struct raw3270_notifier *np;
  1178. rp = cdev->dev.driver_data;
  1179. /*
  1180. * _remove is the opposite of _probe; it's probe that
  1181. * should set up rp. raw3270_remove gets entered for
  1182. * devices even if they haven't been varied online.
  1183. * Thus, rp may validly be NULL here.
  1184. */
  1185. if (rp == NULL)
  1186. return;
  1187. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1188. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1189. /* Deactivate current view and remove all views. */
  1190. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1191. if (rp->view) {
  1192. rp->view->fn->deactivate(rp->view);
  1193. rp->view = NULL;
  1194. }
  1195. while (!list_empty(&rp->view_list)) {
  1196. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1197. if (v->fn->release)
  1198. v->fn->release(v);
  1199. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1200. raw3270_del_view(v);
  1201. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1202. }
  1203. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1204. mutex_lock(&raw3270_mutex);
  1205. list_for_each_entry(np, &raw3270_notifier, list)
  1206. np->notifier(rp->minor, 0);
  1207. mutex_unlock(&raw3270_mutex);
  1208. /* Reset 3270 device. */
  1209. raw3270_reset_device(rp);
  1210. /* And finally remove it. */
  1211. raw3270_delete_device(rp);
  1212. }
  1213. /*
  1214. * Set 3270 device offline.
  1215. */
  1216. static int
  1217. raw3270_set_offline (struct ccw_device *cdev)
  1218. {
  1219. struct raw3270 *rp;
  1220. rp = cdev->dev.driver_data;
  1221. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1222. return -EBUSY;
  1223. raw3270_remove(cdev);
  1224. return 0;
  1225. }
  1226. static struct ccw_device_id raw3270_id[] = {
  1227. { CCW_DEVICE(0x3270, 0) },
  1228. { CCW_DEVICE(0x3271, 0) },
  1229. { CCW_DEVICE(0x3272, 0) },
  1230. { CCW_DEVICE(0x3273, 0) },
  1231. { CCW_DEVICE(0x3274, 0) },
  1232. { CCW_DEVICE(0x3275, 0) },
  1233. { CCW_DEVICE(0x3276, 0) },
  1234. { CCW_DEVICE(0x3277, 0) },
  1235. { CCW_DEVICE(0x3278, 0) },
  1236. { CCW_DEVICE(0x3279, 0) },
  1237. { CCW_DEVICE(0x3174, 0) },
  1238. { /* end of list */ },
  1239. };
  1240. static struct ccw_driver raw3270_ccw_driver = {
  1241. .name = "3270",
  1242. .owner = THIS_MODULE,
  1243. .ids = raw3270_id,
  1244. .probe = &raw3270_probe,
  1245. .remove = &raw3270_remove,
  1246. .set_online = &raw3270_set_online,
  1247. .set_offline = &raw3270_set_offline,
  1248. };
  1249. static int
  1250. raw3270_init(void)
  1251. {
  1252. struct raw3270 *rp;
  1253. int rc;
  1254. if (raw3270_registered)
  1255. return 0;
  1256. raw3270_registered = 1;
  1257. rc = ccw_driver_register(&raw3270_ccw_driver);
  1258. if (rc == 0) {
  1259. /* Create attributes for early (= console) device. */
  1260. mutex_lock(&raw3270_mutex);
  1261. class3270 = class_create(THIS_MODULE, "3270");
  1262. list_for_each_entry(rp, &raw3270_devices, list) {
  1263. get_device(&rp->cdev->dev);
  1264. raw3270_create_attributes(rp);
  1265. }
  1266. mutex_unlock(&raw3270_mutex);
  1267. }
  1268. return rc;
  1269. }
  1270. static void
  1271. raw3270_exit(void)
  1272. {
  1273. ccw_driver_unregister(&raw3270_ccw_driver);
  1274. class_destroy(class3270);
  1275. }
  1276. MODULE_LICENSE("GPL");
  1277. module_init(raw3270_init);
  1278. module_exit(raw3270_exit);
  1279. EXPORT_SYMBOL(raw3270_request_alloc);
  1280. EXPORT_SYMBOL(raw3270_request_free);
  1281. EXPORT_SYMBOL(raw3270_request_reset);
  1282. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1283. EXPORT_SYMBOL(raw3270_request_add_data);
  1284. EXPORT_SYMBOL(raw3270_request_set_data);
  1285. EXPORT_SYMBOL(raw3270_request_set_idal);
  1286. EXPORT_SYMBOL(raw3270_buffer_address);
  1287. EXPORT_SYMBOL(raw3270_add_view);
  1288. EXPORT_SYMBOL(raw3270_del_view);
  1289. EXPORT_SYMBOL(raw3270_find_view);
  1290. EXPORT_SYMBOL(raw3270_activate_view);
  1291. EXPORT_SYMBOL(raw3270_deactivate_view);
  1292. EXPORT_SYMBOL(raw3270_start);
  1293. EXPORT_SYMBOL(raw3270_start_locked);
  1294. EXPORT_SYMBOL(raw3270_start_irq);
  1295. EXPORT_SYMBOL(raw3270_reset);
  1296. EXPORT_SYMBOL(raw3270_register_notifier);
  1297. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1298. EXPORT_SYMBOL(raw3270_wait_queue);