]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/s390/cio/chsc_sch.c
[S390] css: move chsc_private to drv_data
[karo-tx-linux.git] / drivers / s390 / cio / chsc_sch.c
1 /*
2  * Driver for s390 chsc subchannels
3  *
4  * Copyright IBM Corp. 2008, 2009
5  *
6  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7  *
8  */
9
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/miscdevice.h>
15
16 #include <asm/compat.h>
17 #include <asm/cio.h>
18 #include <asm/chsc.h>
19 #include <asm/isc.h>
20
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "css.h"
24 #include "chsc_sch.h"
25 #include "ioasm.h"
26
27 static debug_info_t *chsc_debug_msg_id;
28 static debug_info_t *chsc_debug_log_id;
29
30 #define CHSC_MSG(imp, args...) do {                                     \
31                 debug_sprintf_event(chsc_debug_msg_id, imp , ##args);   \
32         } while (0)
33
34 #define CHSC_LOG(imp, txt) do {                                 \
35                 debug_text_event(chsc_debug_log_id, imp , txt); \
36         } while (0)
37
38 static void CHSC_LOG_HEX(int level, void *data, int length)
39 {
40         while (length > 0) {
41                 debug_event(chsc_debug_log_id, level, data, length);
42                 length -= chsc_debug_log_id->buf_size;
43                 data += chsc_debug_log_id->buf_size;
44         }
45 }
46
47 MODULE_AUTHOR("IBM Corporation");
48 MODULE_DESCRIPTION("driver for s390 chsc subchannels");
49 MODULE_LICENSE("GPL");
50
51 static void chsc_subchannel_irq(struct subchannel *sch)
52 {
53         struct chsc_private *private = dev_get_drvdata(&sch->dev);
54         struct chsc_request *request = private->request;
55         struct irb *irb = (struct irb *)&S390_lowcore.irb;
56
57         CHSC_LOG(4, "irb");
58         CHSC_LOG_HEX(4, irb, sizeof(*irb));
59         /* Copy irb to provided request and set done. */
60         if (!request) {
61                 CHSC_MSG(0, "Interrupt on sch 0.%x.%04x with no request\n",
62                          sch->schid.ssid, sch->schid.sch_no);
63                 return;
64         }
65         private->request = NULL;
66         memcpy(&request->irb, irb, sizeof(*irb));
67         cio_update_schib(sch);
68         complete(&request->completion);
69         put_device(&sch->dev);
70 }
71
72 static int chsc_subchannel_probe(struct subchannel *sch)
73 {
74         struct chsc_private *private;
75         int ret;
76
77         CHSC_MSG(6, "Detected chsc subchannel 0.%x.%04x\n",
78                  sch->schid.ssid, sch->schid.sch_no);
79         sch->isc = CHSC_SCH_ISC;
80         private = kzalloc(sizeof(*private), GFP_KERNEL);
81         if (!private)
82                 return -ENOMEM;
83         dev_set_drvdata(&sch->dev, private);
84         ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
85         if (ret) {
86                 CHSC_MSG(0, "Failed to enable 0.%x.%04x: %d\n",
87                          sch->schid.ssid, sch->schid.sch_no, ret);
88                 dev_set_drvdata(&sch->dev, NULL);
89                 kfree(private);
90         } else {
91                 if (dev_get_uevent_suppress(&sch->dev)) {
92                         dev_set_uevent_suppress(&sch->dev, 0);
93                         kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
94                 }
95         }
96         return ret;
97 }
98
99 static int chsc_subchannel_remove(struct subchannel *sch)
100 {
101         struct chsc_private *private;
102
103         cio_disable_subchannel(sch);
104         private = dev_get_drvdata(&sch->dev);
105         dev_set_drvdata(&sch->dev, NULL);
106         if (private->request) {
107                 complete(&private->request->completion);
108                 put_device(&sch->dev);
109         }
110         kfree(private);
111         return 0;
112 }
113
114 static void chsc_subchannel_shutdown(struct subchannel *sch)
115 {
116         cio_disable_subchannel(sch);
117 }
118
119 static int chsc_subchannel_prepare(struct subchannel *sch)
120 {
121         int cc;
122         struct schib schib;
123         /*
124          * Don't allow suspend while the subchannel is not idle
125          * since we don't have a way to clear the subchannel and
126          * cannot disable it with a request running.
127          */
128         cc = stsch_err(sch->schid, &schib);
129         if (!cc && scsw_stctl(&schib.scsw))
130                 return -EAGAIN;
131         return 0;
132 }
133
134 static int chsc_subchannel_freeze(struct subchannel *sch)
135 {
136         return cio_disable_subchannel(sch);
137 }
138
139 static int chsc_subchannel_restore(struct subchannel *sch)
140 {
141         return cio_enable_subchannel(sch, (u32)(unsigned long)sch);
142 }
143
144 static struct css_device_id chsc_subchannel_ids[] = {
145         { .match_flags = 0x1, .type =SUBCHANNEL_TYPE_CHSC, },
146         { /* end of list */ },
147 };
148 MODULE_DEVICE_TABLE(css, chsc_subchannel_ids);
149
150 static struct css_driver chsc_subchannel_driver = {
151         .owner = THIS_MODULE,
152         .subchannel_type = chsc_subchannel_ids,
153         .irq = chsc_subchannel_irq,
154         .probe = chsc_subchannel_probe,
155         .remove = chsc_subchannel_remove,
156         .shutdown = chsc_subchannel_shutdown,
157         .prepare = chsc_subchannel_prepare,
158         .freeze = chsc_subchannel_freeze,
159         .thaw = chsc_subchannel_restore,
160         .restore = chsc_subchannel_restore,
161         .name = "chsc_subchannel",
162 };
163
164 static int __init chsc_init_dbfs(void)
165 {
166         chsc_debug_msg_id = debug_register("chsc_msg", 16, 1,
167                                            16 * sizeof(long));
168         if (!chsc_debug_msg_id)
169                 goto out;
170         debug_register_view(chsc_debug_msg_id, &debug_sprintf_view);
171         debug_set_level(chsc_debug_msg_id, 2);
172         chsc_debug_log_id = debug_register("chsc_log", 16, 1, 16);
173         if (!chsc_debug_log_id)
174                 goto out;
175         debug_register_view(chsc_debug_log_id, &debug_hex_ascii_view);
176         debug_set_level(chsc_debug_log_id, 2);
177         return 0;
178 out:
179         if (chsc_debug_msg_id)
180                 debug_unregister(chsc_debug_msg_id);
181         return -ENOMEM;
182 }
183
184 static void chsc_remove_dbfs(void)
185 {
186         debug_unregister(chsc_debug_log_id);
187         debug_unregister(chsc_debug_msg_id);
188 }
189
190 static int __init chsc_init_sch_driver(void)
191 {
192         return css_driver_register(&chsc_subchannel_driver);
193 }
194
195 static void chsc_cleanup_sch_driver(void)
196 {
197         css_driver_unregister(&chsc_subchannel_driver);
198 }
199
200 static DEFINE_SPINLOCK(chsc_lock);
201
202 static int chsc_subchannel_match_next_free(struct device *dev, void *data)
203 {
204         struct subchannel *sch = to_subchannel(dev);
205
206         return sch->schib.pmcw.ena && !scsw_fctl(&sch->schib.scsw);
207 }
208
209 static struct subchannel *chsc_get_next_subchannel(struct subchannel *sch)
210 {
211         struct device *dev;
212
213         dev = driver_find_device(&chsc_subchannel_driver.drv,
214                                  sch ? &sch->dev : NULL, NULL,
215                                  chsc_subchannel_match_next_free);
216         return dev ? to_subchannel(dev) : NULL;
217 }
218
219 /**
220  * chsc_async() - try to start a chsc request asynchronously
221  * @chsc_area: request to be started
222  * @request: request structure to associate
223  *
224  * Tries to start a chsc request on one of the existing chsc subchannels.
225  * Returns:
226  *  %0 if the request was performed synchronously
227  *  %-EINPROGRESS if the request was successfully started
228  *  %-EBUSY if all chsc subchannels are busy
229  *  %-ENODEV if no chsc subchannels are available
230  * Context:
231  *  interrupts disabled, chsc_lock held
232  */
233 static int chsc_async(struct chsc_async_area *chsc_area,
234                       struct chsc_request *request)
235 {
236         int cc;
237         struct chsc_private *private;
238         struct subchannel *sch = NULL;
239         int ret = -ENODEV;
240         char dbf[10];
241
242         chsc_area->header.key = PAGE_DEFAULT_KEY >> 4;
243         while ((sch = chsc_get_next_subchannel(sch))) {
244                 spin_lock(sch->lock);
245                 private = dev_get_drvdata(&sch->dev);
246                 if (private->request) {
247                         spin_unlock(sch->lock);
248                         ret = -EBUSY;
249                         continue;
250                 }
251                 chsc_area->header.sid = sch->schid;
252                 CHSC_LOG(2, "schid");
253                 CHSC_LOG_HEX(2, &sch->schid, sizeof(sch->schid));
254                 cc = chsc(chsc_area);
255                 sprintf(dbf, "cc:%d", cc);
256                 CHSC_LOG(2, dbf);
257                 switch (cc) {
258                 case 0:
259                         ret = 0;
260                         break;
261                 case 1:
262                         sch->schib.scsw.cmd.fctl |= SCSW_FCTL_START_FUNC;
263                         ret = -EINPROGRESS;
264                         private->request = request;
265                         break;
266                 case 2:
267                         ret = -EBUSY;
268                         break;
269                 default:
270                         ret = -ENODEV;
271                 }
272                 spin_unlock(sch->lock);
273                 CHSC_MSG(2, "chsc on 0.%x.%04x returned cc=%d\n",
274                          sch->schid.ssid, sch->schid.sch_no, cc);
275                 if (ret == -EINPROGRESS)
276                         return -EINPROGRESS;
277                 put_device(&sch->dev);
278                 if (ret == 0)
279                         return 0;
280         }
281         return ret;
282 }
283
284 static void chsc_log_command(struct chsc_async_area *chsc_area)
285 {
286         char dbf[10];
287
288         sprintf(dbf, "CHSC:%x", chsc_area->header.code);
289         CHSC_LOG(0, dbf);
290         CHSC_LOG_HEX(0, chsc_area, 32);
291 }
292
293 static int chsc_examine_irb(struct chsc_request *request)
294 {
295         int backed_up;
296
297         if (!(scsw_stctl(&request->irb.scsw) & SCSW_STCTL_STATUS_PEND))
298                 return -EIO;
299         backed_up = scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHAIN_CHECK;
300         request->irb.scsw.cmd.cstat &= ~SCHN_STAT_CHAIN_CHECK;
301         if (scsw_cstat(&request->irb.scsw) == 0)
302                 return 0;
303         if (!backed_up)
304                 return 0;
305         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROG_CHECK)
306                 return -EIO;
307         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_PROT_CHECK)
308                 return -EPERM;
309         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_DATA_CHK)
310                 return -EAGAIN;
311         if (scsw_cstat(&request->irb.scsw) & SCHN_STAT_CHN_CTRL_CHK)
312                 return -EAGAIN;
313         return -EIO;
314 }
315
316 static int chsc_ioctl_start(void __user *user_area)
317 {
318         struct chsc_request *request;
319         struct chsc_async_area *chsc_area;
320         int ret;
321         char dbf[10];
322
323         if (!css_general_characteristics.dynio)
324                 /* It makes no sense to try. */
325                 return -EOPNOTSUPP;
326         chsc_area = (void *)get_zeroed_page(GFP_DMA | GFP_KERNEL);
327         if (!chsc_area)
328                 return -ENOMEM;
329         request = kzalloc(sizeof(*request), GFP_KERNEL);
330         if (!request) {
331                 ret = -ENOMEM;
332                 goto out_free;
333         }
334         init_completion(&request->completion);
335         if (copy_from_user(chsc_area, user_area, PAGE_SIZE)) {
336                 ret = -EFAULT;
337                 goto out_free;
338         }
339         chsc_log_command(chsc_area);
340         spin_lock_irq(&chsc_lock);
341         ret = chsc_async(chsc_area, request);
342         spin_unlock_irq(&chsc_lock);
343         if (ret == -EINPROGRESS) {
344                 wait_for_completion(&request->completion);
345                 ret = chsc_examine_irb(request);
346         }
347         /* copy area back to user */
348         if (!ret)
349                 if (copy_to_user(user_area, chsc_area, PAGE_SIZE))
350                         ret = -EFAULT;
351 out_free:
352         sprintf(dbf, "ret:%d", ret);
353         CHSC_LOG(0, dbf);
354         kfree(request);
355         free_page((unsigned long)chsc_area);
356         return ret;
357 }
358
359 static int chsc_ioctl_info_channel_path(void __user *user_cd)
360 {
361         struct chsc_chp_cd *cd;
362         int ret, ccode;
363         struct {
364                 struct chsc_header request;
365                 u32 : 2;
366                 u32 m : 1;
367                 u32 : 1;
368                 u32 fmt1 : 4;
369                 u32 cssid : 8;
370                 u32 : 8;
371                 u32 first_chpid : 8;
372                 u32 : 24;
373                 u32 last_chpid : 8;
374                 u32 : 32;
375                 struct chsc_header response;
376                 u8 data[PAGE_SIZE - 20];
377         } __attribute__ ((packed)) *scpcd_area;
378
379         scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
380         if (!scpcd_area)
381                 return -ENOMEM;
382         cd = kzalloc(sizeof(*cd), GFP_KERNEL);
383         if (!cd) {
384                 ret = -ENOMEM;
385                 goto out_free;
386         }
387         if (copy_from_user(cd, user_cd, sizeof(*cd))) {
388                 ret = -EFAULT;
389                 goto out_free;
390         }
391         scpcd_area->request.length = 0x0010;
392         scpcd_area->request.code = 0x0028;
393         scpcd_area->m = cd->m;
394         scpcd_area->fmt1 = cd->fmt;
395         scpcd_area->cssid = cd->chpid.cssid;
396         scpcd_area->first_chpid = cd->chpid.id;
397         scpcd_area->last_chpid = cd->chpid.id;
398
399         ccode = chsc(scpcd_area);
400         if (ccode != 0) {
401                 ret = -EIO;
402                 goto out_free;
403         }
404         if (scpcd_area->response.code != 0x0001) {
405                 ret = -EIO;
406                 CHSC_MSG(0, "scpcd: response code=%x\n",
407                          scpcd_area->response.code);
408                 goto out_free;
409         }
410         memcpy(&cd->cpcb, &scpcd_area->response, scpcd_area->response.length);
411         if (copy_to_user(user_cd, cd, sizeof(*cd)))
412                 ret = -EFAULT;
413         else
414                 ret = 0;
415 out_free:
416         kfree(cd);
417         free_page((unsigned long)scpcd_area);
418         return ret;
419 }
420
421 static int chsc_ioctl_info_cu(void __user *user_cd)
422 {
423         struct chsc_cu_cd *cd;
424         int ret, ccode;
425         struct {
426                 struct chsc_header request;
427                 u32 : 2;
428                 u32 m : 1;
429                 u32 : 1;
430                 u32 fmt1 : 4;
431                 u32 cssid : 8;
432                 u32 : 8;
433                 u32 first_cun : 8;
434                 u32 : 24;
435                 u32 last_cun : 8;
436                 u32 : 32;
437                 struct chsc_header response;
438                 u8 data[PAGE_SIZE - 20];
439         } __attribute__ ((packed)) *scucd_area;
440
441         scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
442         if (!scucd_area)
443                 return -ENOMEM;
444         cd = kzalloc(sizeof(*cd), GFP_KERNEL);
445         if (!cd) {
446                 ret = -ENOMEM;
447                 goto out_free;
448         }
449         if (copy_from_user(cd, user_cd, sizeof(*cd))) {
450                 ret = -EFAULT;
451                 goto out_free;
452         }
453         scucd_area->request.length = 0x0010;
454         scucd_area->request.code = 0x0028;
455         scucd_area->m = cd->m;
456         scucd_area->fmt1 = cd->fmt;
457         scucd_area->cssid = cd->cssid;
458         scucd_area->first_cun = cd->cun;
459         scucd_area->last_cun = cd->cun;
460
461         ccode = chsc(scucd_area);
462         if (ccode != 0) {
463                 ret = -EIO;
464                 goto out_free;
465         }
466         if (scucd_area->response.code != 0x0001) {
467                 ret = -EIO;
468                 CHSC_MSG(0, "scucd: response code=%x\n",
469                          scucd_area->response.code);
470                 goto out_free;
471         }
472         memcpy(&cd->cucb, &scucd_area->response, scucd_area->response.length);
473         if (copy_to_user(user_cd, cd, sizeof(*cd)))
474                 ret = -EFAULT;
475         else
476                 ret = 0;
477 out_free:
478         kfree(cd);
479         free_page((unsigned long)scucd_area);
480         return ret;
481 }
482
483 static int chsc_ioctl_info_sch_cu(void __user *user_cud)
484 {
485         struct chsc_sch_cud *cud;
486         int ret, ccode;
487         struct {
488                 struct chsc_header request;
489                 u32 : 2;
490                 u32 m : 1;
491                 u32 : 5;
492                 u32 fmt1 : 4;
493                 u32 : 2;
494                 u32 ssid : 2;
495                 u32 first_sch : 16;
496                 u32 : 8;
497                 u32 cssid : 8;
498                 u32 last_sch : 16;
499                 u32 : 32;
500                 struct chsc_header response;
501                 u8 data[PAGE_SIZE - 20];
502         } __attribute__ ((packed)) *sscud_area;
503
504         sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
505         if (!sscud_area)
506                 return -ENOMEM;
507         cud = kzalloc(sizeof(*cud), GFP_KERNEL);
508         if (!cud) {
509                 ret = -ENOMEM;
510                 goto out_free;
511         }
512         if (copy_from_user(cud, user_cud, sizeof(*cud))) {
513                 ret = -EFAULT;
514                 goto out_free;
515         }
516         sscud_area->request.length = 0x0010;
517         sscud_area->request.code = 0x0006;
518         sscud_area->m = cud->schid.m;
519         sscud_area->fmt1 = cud->fmt;
520         sscud_area->ssid = cud->schid.ssid;
521         sscud_area->first_sch = cud->schid.sch_no;
522         sscud_area->cssid = cud->schid.cssid;
523         sscud_area->last_sch = cud->schid.sch_no;
524
525         ccode = chsc(sscud_area);
526         if (ccode != 0) {
527                 ret = -EIO;
528                 goto out_free;
529         }
530         if (sscud_area->response.code != 0x0001) {
531                 ret = -EIO;
532                 CHSC_MSG(0, "sscud: response code=%x\n",
533                          sscud_area->response.code);
534                 goto out_free;
535         }
536         memcpy(&cud->scub, &sscud_area->response, sscud_area->response.length);
537         if (copy_to_user(user_cud, cud, sizeof(*cud)))
538                 ret = -EFAULT;
539         else
540                 ret = 0;
541 out_free:
542         kfree(cud);
543         free_page((unsigned long)sscud_area);
544         return ret;
545 }
546
547 static int chsc_ioctl_conf_info(void __user *user_ci)
548 {
549         struct chsc_conf_info *ci;
550         int ret, ccode;
551         struct {
552                 struct chsc_header request;
553                 u32 : 2;
554                 u32 m : 1;
555                 u32 : 1;
556                 u32 fmt1 : 4;
557                 u32 cssid : 8;
558                 u32 : 6;
559                 u32 ssid : 2;
560                 u32 : 8;
561                 u64 : 64;
562                 struct chsc_header response;
563                 u8 data[PAGE_SIZE - 20];
564         } __attribute__ ((packed)) *sci_area;
565
566         sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
567         if (!sci_area)
568                 return -ENOMEM;
569         ci = kzalloc(sizeof(*ci), GFP_KERNEL);
570         if (!ci) {
571                 ret = -ENOMEM;
572                 goto out_free;
573         }
574         if (copy_from_user(ci, user_ci, sizeof(*ci))) {
575                 ret = -EFAULT;
576                 goto out_free;
577         }
578         sci_area->request.length = 0x0010;
579         sci_area->request.code = 0x0012;
580         sci_area->m = ci->id.m;
581         sci_area->fmt1 = ci->fmt;
582         sci_area->cssid = ci->id.cssid;
583         sci_area->ssid = ci->id.ssid;
584
585         ccode = chsc(sci_area);
586         if (ccode != 0) {
587                 ret = -EIO;
588                 goto out_free;
589         }
590         if (sci_area->response.code != 0x0001) {
591                 ret = -EIO;
592                 CHSC_MSG(0, "sci: response code=%x\n",
593                          sci_area->response.code);
594                 goto out_free;
595         }
596         memcpy(&ci->scid, &sci_area->response, sci_area->response.length);
597         if (copy_to_user(user_ci, ci, sizeof(*ci)))
598                 ret = -EFAULT;
599         else
600                 ret = 0;
601 out_free:
602         kfree(ci);
603         free_page((unsigned long)sci_area);
604         return ret;
605 }
606
607 static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
608 {
609         struct chsc_comp_list *ccl;
610         int ret, ccode;
611         struct {
612                 struct chsc_header request;
613                 u32 ctype : 8;
614                 u32 : 4;
615                 u32 fmt : 4;
616                 u32 : 16;
617                 u64 : 64;
618                 u32 list_parm[2];
619                 u64 : 64;
620                 struct chsc_header response;
621                 u8 data[PAGE_SIZE - 36];
622         } __attribute__ ((packed)) *sccl_area;
623         struct {
624                 u32 m : 1;
625                 u32 : 31;
626                 u32 cssid : 8;
627                 u32 : 16;
628                 u32 chpid : 8;
629         } __attribute__ ((packed)) *chpid_parm;
630         struct {
631                 u32 f_cssid : 8;
632                 u32 l_cssid : 8;
633                 u32 : 16;
634                 u32 res;
635         } __attribute__ ((packed)) *cssids_parm;
636
637         sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
638         if (!sccl_area)
639                 return -ENOMEM;
640         ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
641         if (!ccl) {
642                 ret = -ENOMEM;
643                 goto out_free;
644         }
645         if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
646                 ret = -EFAULT;
647                 goto out_free;
648         }
649         sccl_area->request.length = 0x0020;
650         sccl_area->request.code = 0x0030;
651         sccl_area->fmt = ccl->req.fmt;
652         sccl_area->ctype = ccl->req.ctype;
653         switch (sccl_area->ctype) {
654         case CCL_CU_ON_CHP:
655         case CCL_IOP_CHP:
656                 chpid_parm = (void *)&sccl_area->list_parm;
657                 chpid_parm->m = ccl->req.chpid.m;
658                 chpid_parm->cssid = ccl->req.chpid.chp.cssid;
659                 chpid_parm->chpid = ccl->req.chpid.chp.id;
660                 break;
661         case CCL_CSS_IMG:
662         case CCL_CSS_IMG_CONF_CHAR:
663                 cssids_parm = (void *)&sccl_area->list_parm;
664                 cssids_parm->f_cssid = ccl->req.cssids.f_cssid;
665                 cssids_parm->l_cssid = ccl->req.cssids.l_cssid;
666                 break;
667         }
668         ccode = chsc(sccl_area);
669         if (ccode != 0) {
670                 ret = -EIO;
671                 goto out_free;
672         }
673         if (sccl_area->response.code != 0x0001) {
674                 ret = -EIO;
675                 CHSC_MSG(0, "sccl: response code=%x\n",
676                          sccl_area->response.code);
677                 goto out_free;
678         }
679         memcpy(&ccl->sccl, &sccl_area->response, sccl_area->response.length);
680         if (copy_to_user(user_ccl, ccl, sizeof(*ccl)))
681                 ret = -EFAULT;
682         else
683                 ret = 0;
684 out_free:
685         kfree(ccl);
686         free_page((unsigned long)sccl_area);
687         return ret;
688 }
689
690 static int chsc_ioctl_chpd(void __user *user_chpd)
691 {
692         struct chsc_scpd *scpd_area;
693         struct chsc_cpd_info *chpd;
694         int ret;
695
696         chpd = kzalloc(sizeof(*chpd), GFP_KERNEL);
697         scpd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
698         if (!scpd_area || !chpd) {
699                 ret = -ENOMEM;
700                 goto out_free;
701         }
702         if (copy_from_user(chpd, user_chpd, sizeof(*chpd))) {
703                 ret = -EFAULT;
704                 goto out_free;
705         }
706         ret = chsc_determine_channel_path_desc(chpd->chpid, chpd->fmt,
707                                                chpd->rfmt, chpd->c, chpd->m,
708                                                scpd_area);
709         if (ret)
710                 goto out_free;
711         memcpy(&chpd->chpdb, &scpd_area->response, scpd_area->response.length);
712         if (copy_to_user(user_chpd, chpd, sizeof(*chpd)))
713                 ret = -EFAULT;
714 out_free:
715         kfree(chpd);
716         free_page((unsigned long)scpd_area);
717         return ret;
718 }
719
720 static int chsc_ioctl_dcal(void __user *user_dcal)
721 {
722         struct chsc_dcal *dcal;
723         int ret, ccode;
724         struct {
725                 struct chsc_header request;
726                 u32 atype : 8;
727                 u32 : 4;
728                 u32 fmt : 4;
729                 u32 : 16;
730                 u32 res0[2];
731                 u32 list_parm[2];
732                 u32 res1[2];
733                 struct chsc_header response;
734                 u8 data[PAGE_SIZE - 36];
735         } __attribute__ ((packed)) *sdcal_area;
736
737         sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
738         if (!sdcal_area)
739                 return -ENOMEM;
740         dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
741         if (!dcal) {
742                 ret = -ENOMEM;
743                 goto out_free;
744         }
745         if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
746                 ret = -EFAULT;
747                 goto out_free;
748         }
749         sdcal_area->request.length = 0x0020;
750         sdcal_area->request.code = 0x0034;
751         sdcal_area->atype = dcal->req.atype;
752         sdcal_area->fmt = dcal->req.fmt;
753         memcpy(&sdcal_area->list_parm, &dcal->req.list_parm,
754                sizeof(sdcal_area->list_parm));
755
756         ccode = chsc(sdcal_area);
757         if (ccode != 0) {
758                 ret = -EIO;
759                 goto out_free;
760         }
761         if (sdcal_area->response.code != 0x0001) {
762                 ret = -EIO;
763                 CHSC_MSG(0, "sdcal: response code=%x\n",
764                          sdcal_area->response.code);
765                 goto out_free;
766         }
767         memcpy(&dcal->sdcal, &sdcal_area->response,
768                sdcal_area->response.length);
769         if (copy_to_user(user_dcal, dcal, sizeof(*dcal)))
770                 ret = -EFAULT;
771         else
772                 ret = 0;
773 out_free:
774         kfree(dcal);
775         free_page((unsigned long)sdcal_area);
776         return ret;
777 }
778
779 static long chsc_ioctl(struct file *filp, unsigned int cmd,
780                        unsigned long arg)
781 {
782         void __user *argp;
783
784         CHSC_MSG(2, "chsc_ioctl called, cmd=%x\n", cmd);
785         if (is_compat_task())
786                 argp = compat_ptr(arg);
787         else
788                 argp = (void __user *)arg;
789         switch (cmd) {
790         case CHSC_START:
791                 return chsc_ioctl_start(argp);
792         case CHSC_INFO_CHANNEL_PATH:
793                 return chsc_ioctl_info_channel_path(argp);
794         case CHSC_INFO_CU:
795                 return chsc_ioctl_info_cu(argp);
796         case CHSC_INFO_SCH_CU:
797                 return chsc_ioctl_info_sch_cu(argp);
798         case CHSC_INFO_CI:
799                 return chsc_ioctl_conf_info(argp);
800         case CHSC_INFO_CCL:
801                 return chsc_ioctl_conf_comp_list(argp);
802         case CHSC_INFO_CPD:
803                 return chsc_ioctl_chpd(argp);
804         case CHSC_INFO_DCAL:
805                 return chsc_ioctl_dcal(argp);
806         default: /* unknown ioctl number */
807                 return -ENOIOCTLCMD;
808         }
809 }
810
811 static const struct file_operations chsc_fops = {
812         .owner = THIS_MODULE,
813         .open = nonseekable_open,
814         .unlocked_ioctl = chsc_ioctl,
815         .compat_ioctl = chsc_ioctl,
816         .llseek = no_llseek,
817 };
818
819 static struct miscdevice chsc_misc_device = {
820         .minor = MISC_DYNAMIC_MINOR,
821         .name = "chsc",
822         .fops = &chsc_fops,
823 };
824
825 static int __init chsc_misc_init(void)
826 {
827         return misc_register(&chsc_misc_device);
828 }
829
830 static void chsc_misc_cleanup(void)
831 {
832         misc_deregister(&chsc_misc_device);
833 }
834
835 static int __init chsc_sch_init(void)
836 {
837         int ret;
838
839         ret = chsc_init_dbfs();
840         if (ret)
841                 return ret;
842         isc_register(CHSC_SCH_ISC);
843         ret = chsc_init_sch_driver();
844         if (ret)
845                 goto out_dbf;
846         ret = chsc_misc_init();
847         if (ret)
848                 goto out_driver;
849         return ret;
850 out_driver:
851         chsc_cleanup_sch_driver();
852 out_dbf:
853         isc_unregister(CHSC_SCH_ISC);
854         chsc_remove_dbfs();
855         return ret;
856 }
857
858 static void __exit chsc_sch_exit(void)
859 {
860         chsc_misc_cleanup();
861         chsc_cleanup_sch_driver();
862         isc_unregister(CHSC_SCH_ISC);
863         chsc_remove_dbfs();
864 }
865
866 module_init(chsc_sch_init);
867 module_exit(chsc_sch_exit);