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