ucmb: Allow chunk-ing big transfers to reduce latency issues.
[packages.git] / utils / ucmb / driver / ucmb.c
1 /*
2  *   Microcontroller Message Bus
3  *   Linux kernel driver
4  *
5  *   Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
6  *
7  *   This program is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU General Public License
9  *   as published by the Free Software Foundation; either version 2
10  *   of the License, or (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  */
17
18 #include "ucmb.h"
19
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/miscdevice.h>
23 #include <linux/fs.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_gpio.h>
26 #include <linux/spi/spi_bitbang.h>
27 #include <linux/gpio.h>
28 #include <linux/gfp.h>
29 #include <linux/delay.h>
30 #include <linux/crc16.h>
31 #include <linux/sched.h>
32
33 #include <asm/uaccess.h>
34
35
36 #define PFX     "ucmb: "
37
38 #undef DEBUG
39
40
41 MODULE_LICENSE("GPL");
42 MODULE_DESCRIPTION("Microcontroller Message Bus");
43 MODULE_AUTHOR("Michael Buesch");
44
45
46 struct ucmb {
47         struct mutex mutex;
48
49         bool is_open;
50
51         unsigned int chunk_size;
52         unsigned int msg_delay_usec;
53         unsigned int gpio_reset;
54         bool reset_activelow;
55
56         /* Misc character device driver */
57         struct miscdevice mdev;
58         struct file_operations mdev_fops;
59
60         /* SPI driver */
61         struct spi_device *sdev;
62
63         /* SPI-GPIO driver */
64         struct spi_gpio_platform_data spi_gpio_pdata;
65         struct platform_device spi_gpio_pdev;
66 };
67
68 #define UCMB_MAX_MSG_DELAY      (10 * 1000 * 1000) /* 10 seconds */
69
70
71 struct ucmb_message_hdr {
72         __le16 magic;           /* UCMB_MAGIC */
73         __le16 len;             /* Payload length (excluding header and footer) */
74 } __attribute__((packed));
75
76 struct ucmb_message_footer {
77         __le16 crc;             /* CRC of the header + payload. */
78 } __attribute__((packed));
79
80 struct ucmb_status {
81         __le16 magic;           /* UCMB_MAGIC */
82         __le16 code;            /* enum ucmb_status_code */
83 } __attribute__((packed));
84
85 #define UCMB_MAGIC              0x1337
86
87 enum ucmb_status_code {
88         UCMB_STAT_OK = 0,
89         UCMB_STAT_EPROTO,       /* Protocol format error */
90         UCMB_STAT_ENOMEM,       /* Out of memory */
91         UCMB_STAT_E2BIG,        /* Message too big */
92         UCMB_STAT_ECRC,         /* CRC error */
93 };
94
95
96 static int ucmb_spi_busnum_count = 1337;
97 static int ucmb_pdev_id_count;
98
99
100 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
101 {
102         return 0;
103 }
104
105 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
106 {
107         return 0;
108 }
109
110 static struct spi_driver ucmb_spi_driver = {
111         .driver         = {
112                 .name   = "ucmb",
113                 .bus    = &spi_bus_type,
114                 .owner  = THIS_MODULE,
115         },
116         .probe          = ucmb_spi_probe,
117         .remove         = __devexit_p(ucmb_spi_remove),
118 };
119
120 static void ucmb_toggle_reset_line(struct ucmb *ucmb, bool active)
121 {
122         if (ucmb->reset_activelow)
123                 active = !active;
124         gpio_set_value(ucmb->gpio_reset, active);
125 }
126
127 static int ucmb_reset_microcontroller(struct ucmb *ucmb)
128 {
129         if (ucmb->gpio_reset == UCMB_NO_RESET)
130                 return -ENODEV;
131
132         ucmb_toggle_reset_line(ucmb, 1);
133         msleep(50);
134         ucmb_toggle_reset_line(ucmb, 0);
135         msleep(50);
136
137         return 0;
138 }
139
140 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
141 {
142         switch (code) {
143         case UCMB_STAT_OK:
144                 return 0;
145         case UCMB_STAT_EPROTO:
146                 return -EPROTO;
147         case UCMB_STAT_ENOMEM:
148                 return -ENOMEM;
149         case UCMB_STAT_E2BIG:
150                 return -E2BIG;
151         case UCMB_STAT_ECRC:
152                 return -EBADMSG;
153         }
154         return -EBUSY;
155 }
156
157 static inline struct ucmb * filp_to_ucmb(struct file *filp)
158 {
159         return container_of(filp->f_op, struct ucmb, mdev_fops);
160 }
161
162 static int ucmb_open(struct inode *inode, struct file *filp)
163 {
164         struct ucmb *ucmb = filp_to_ucmb(filp);
165         int err = 0;
166
167         mutex_lock(&ucmb->mutex);
168
169         if (ucmb->is_open) {
170                 err = -EBUSY;
171                 goto out_unlock;
172         }
173         ucmb->is_open = 1;
174         ucmb->msg_delay_usec = 0;
175
176 out_unlock:
177         mutex_unlock(&ucmb->mutex);
178
179         return err;
180 }
181
182 static int ucmb_release(struct inode *inode, struct file *filp)
183 {
184         struct ucmb *ucmb = filp_to_ucmb(filp);
185
186         mutex_lock(&ucmb->mutex);
187         WARN_ON(!ucmb->is_open);
188         ucmb->is_open = 0;
189         mutex_unlock(&ucmb->mutex);
190
191         return 0;
192 }
193
194 static int ucmb_ioctl(struct inode *inode, struct file *filp,
195                       unsigned int cmd, unsigned long arg)
196 {
197         struct ucmb *ucmb = filp_to_ucmb(filp);
198         int ret = 0;
199
200         mutex_lock(&ucmb->mutex);
201         switch (cmd) {
202         case UCMB_IOCTL_RESETUC:
203                 ret = ucmb_reset_microcontroller(ucmb);
204                 break;
205         case UCMB_IOCTL_GMSGDELAY:
206                 if (put_user(ucmb->msg_delay_usec, (unsigned int __user *)arg)) {
207                         ret = -EFAULT;
208                         break;
209                 }
210                 break;
211         case UCMB_IOCTL_SMSGDELAY: {
212                 unsigned int msg_delay_usec;
213
214                 if (get_user(msg_delay_usec, (unsigned int __user *)arg)) {
215                         ret = -EFAULT;
216                         break;
217                 }
218                 if (msg_delay_usec > UCMB_MAX_MSG_DELAY) {
219                         ret = -E2BIG;
220                         break;
221                 }
222                 ucmb->msg_delay_usec = msg_delay_usec;
223                 break;
224         }
225         default:
226                 ret = -EINVAL;
227         }
228         mutex_unlock(&ucmb->mutex);
229
230         return ret;
231 }
232
233 static int ucmb_spi_write(struct ucmb *ucmb, const void *_buf, size_t size)
234 {
235         const u8 *buf = _buf;
236         size_t i, chunk_size, current_size;
237         int err = 0;
238
239         chunk_size = ucmb->chunk_size ? : size;
240         for (i = 0; i < size; i += chunk_size) {
241                 current_size = chunk_size;
242                 if (i + current_size > size)
243                         current_size = size - i;
244                 err = spi_write(ucmb->sdev, buf + i, current_size);
245                 if (err)
246                         goto out;
247                 if (ucmb->chunk_size && need_resched())
248                         msleep(1);
249         }
250 out:
251         return err;
252 }
253
254 static int ucmb_spi_read(struct ucmb *ucmb, void *_buf, size_t size)
255 {
256         u8 *buf = _buf;
257         size_t i, chunk_size, current_size;
258         int err = 0;
259
260         chunk_size = ucmb->chunk_size ? : size;
261         for (i = 0; i < size; i += chunk_size) {
262                 current_size = chunk_size;
263                 if (i + current_size > size)
264                         current_size = size - i;
265                 err = spi_read(ucmb->sdev, buf + i, current_size);
266                 if (err)
267                         goto out;
268                 if (ucmb->chunk_size && need_resched())
269                         msleep(1);
270         }
271 out:
272         return err;
273 }
274
275 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
276                          size_t size, loff_t *offp)
277 {
278         struct ucmb *ucmb = filp_to_ucmb(filp);
279         u8 *buf;
280         int res, err;
281         struct ucmb_message_hdr hdr;
282         struct ucmb_message_footer footer;
283         struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
284         u16 crc = 0xFFFF;
285
286         mutex_lock(&ucmb->mutex);
287
288         size = min_t(size_t, size, PAGE_SIZE);
289
290         err = -ENOMEM;
291         buf = (char *)__get_free_page(GFP_KERNEL);
292         if (!buf)
293                 goto out;
294
295         err = ucmb_spi_read(ucmb, &hdr, sizeof(hdr));
296         if (err)
297                 goto out_free;
298 #ifdef DEBUG
299         printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
300                le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
301 #endif
302         err = -EPROTO;
303         if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
304                 goto out_free;
305         err = -ENOBUFS;
306         if (size < le16_to_cpu(hdr.len))
307                 goto out_free;
308         size = le16_to_cpu(hdr.len);
309         err = ucmb_spi_read(ucmb, buf, size);
310         if (err)
311                 goto out_free;
312         err = ucmb_spi_read(ucmb, &footer, sizeof(footer));
313         if (err)
314                 goto out_free;
315
316         crc = crc16(crc, (u8 *)&hdr, sizeof(hdr));
317         crc = crc16(crc, buf, size);
318         crc ^= 0xFFFF;
319         if (crc != le16_to_cpu(footer.crc)) {
320                 err = -EPROTO;
321                 status.code = UCMB_STAT_ECRC;
322                 goto out_send_status;
323         }
324
325         if (copy_to_user(user_buf, buf, size)) {
326                 err = -EFAULT;
327                 status.code = UCMB_STAT_ENOMEM;
328                 goto out_send_status;
329         }
330
331         status.code = UCMB_STAT_OK;
332         err = 0;
333
334 out_send_status:
335         res = ucmb_spi_write(ucmb, &status, sizeof(status));
336         if (res && !err)
337                 err = res;
338 out_free:
339         free_page((unsigned long)buf);
340 out:
341         mutex_unlock(&ucmb->mutex);
342
343         return err ? err : size;
344 }
345
346 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
347                           size_t size, loff_t *offp)
348 {
349         struct ucmb *ucmb = filp_to_ucmb(filp);
350         u8 *buf;
351         int err;
352         struct ucmb_message_hdr hdr = { .magic = cpu_to_le16(UCMB_MAGIC), };
353         struct ucmb_message_footer footer = { .crc = 0xFFFF, };
354         struct ucmb_status status;
355         size_t i, current_size, chunk_size;
356
357         mutex_lock(&ucmb->mutex);
358
359         err = -ENOMEM;
360         buf = (char *)__get_free_page(GFP_KERNEL);
361         if (!buf)
362                 goto out;
363
364         size = min_t(size_t, PAGE_SIZE, size);
365         err = -EFAULT;
366         if (copy_from_user(buf, user_buf, size))
367                 goto out_free;
368         hdr.len = cpu_to_le16(size);
369
370         footer.crc = crc16(footer.crc, (u8 *)&hdr, sizeof(hdr));
371         footer.crc = crc16(footer.crc, buf, size);
372         footer.crc ^= 0xFFFF;
373
374         err = ucmb_spi_write(ucmb, &hdr, sizeof(hdr));
375         if (err)
376                 goto out_free;
377         err = ucmb_spi_write(ucmb, buf, size);
378         if (err)
379                 goto out_free;
380         err = ucmb_spi_write(ucmb, &footer, sizeof(footer));
381         if (err)
382                 goto out_free;
383
384         if (ucmb->msg_delay_usec) {
385                 /* The microcontroller deserves some time to process the message. */
386                 if (ucmb->msg_delay_usec >= 1000000) {
387                         ssleep(ucmb->msg_delay_usec / 1000000);
388                         msleep(DIV_ROUND_UP(ucmb->msg_delay_usec % 1000000, 1000));
389                 } else if (ucmb->msg_delay_usec >= 1000) {
390                         msleep(DIV_ROUND_UP(ucmb->msg_delay_usec, 1000));
391                 } else
392                         udelay(ucmb->msg_delay_usec);
393         }
394
395         /* Get the status code. */
396         err = ucmb_spi_read(ucmb, &status, sizeof(status));
397         if (err)
398                 goto out_free;
399 #ifdef DEBUG
400         printk(KERN_DEBUG PFX "Sent message. Status report: 0x%04X 0x%04X\n",
401                le16_to_cpu(status.magic), le16_to_cpu(status.code));
402 #endif
403         err = -EPROTO;
404         if (status.magic != cpu_to_le16(UCMB_MAGIC))
405                 goto out_free;
406         err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
407         if (err)
408                 goto out_free;
409
410 out_free:
411         free_page((unsigned long)buf);
412 out:
413         mutex_unlock(&ucmb->mutex);
414
415         return err ? err : size;
416 }
417
418 static int __devinit ucmb_probe(struct platform_device *pdev)
419 {
420         struct ucmb_platform_data *pdata;
421         struct ucmb *ucmb;
422         int err;
423         const int bus_num = ucmb_spi_busnum_count++;
424         struct spi_bitbang *bb;
425
426         pdata = pdev->dev.platform_data;
427         if (!pdata)
428                 return -ENXIO;
429
430         ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
431         if (!ucmb)
432                 return -ENOMEM;
433         mutex_init(&ucmb->mutex);
434         ucmb->gpio_reset = pdata->gpio_reset;
435         ucmb->reset_activelow = pdata->reset_activelow;
436         ucmb->chunk_size = pdata->chunk_size;
437
438 #ifdef CONFIG_PREEMPT
439         /* A preemptible kernel does not need to sleep between
440          * chunks, because it can sleep at desire (if IRQs are enabled).
441          * So transmit/receive it all in one go. */
442         ucmb->chunk_size = 0;
443 #endif
444
445         /* Create the SPI GPIO bus master. */
446
447 #ifdef CONFIG_SPI_GPIO_MODULE
448         err = request_module("spi_gpio");
449         if (err)
450                 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
451 #endif /* CONFIG_SPI_GPIO_MODULE */
452
453         ucmb->spi_gpio_pdata.sck = pdata->gpio_sck;
454         ucmb->spi_gpio_pdata.mosi = pdata->gpio_mosi;
455         ucmb->spi_gpio_pdata.miso = pdata->gpio_miso;
456         ucmb->spi_gpio_pdata.num_chipselect = 1;
457
458         ucmb->spi_gpio_pdev.name = "spi_gpio";
459         ucmb->spi_gpio_pdev.id = bus_num;
460         ucmb->spi_gpio_pdev.dev.platform_data = &ucmb->spi_gpio_pdata;
461
462         err = platform_device_register(&ucmb->spi_gpio_pdev);
463         if (err) {
464                 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
465                 goto err_free_ucmb;
466         }
467         bb = platform_get_drvdata(&ucmb->spi_gpio_pdev);
468         if (!bb || !bb->master) {
469                 printk(KERN_ERR PFX "No bitbanged master device found.\n");
470                 goto err_unreg_spi_gpio_pdev;
471         }
472
473         /* Create the SPI device. */
474
475         ucmb->sdev = spi_alloc_device(bb->master);
476         if (!ucmb->sdev) {
477                 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
478                 goto err_unreg_spi_gpio_pdev;
479         }
480         ucmb->sdev->max_speed_hz = pdata->max_speed_hz;
481         ucmb->sdev->chip_select = 0;
482         ucmb->sdev->mode = pdata->mode;
483         strlcpy(ucmb->sdev->modalias, "ucmb", /* We are the SPI driver. */
484                 sizeof(ucmb->sdev->modalias));
485         ucmb->sdev->controller_data = (void *)pdata->gpio_cs;
486         err = spi_add_device(ucmb->sdev);
487         if (err) {
488                 printk(KERN_ERR PFX "Failed to add SPI device\n");
489                 goto err_free_spi_device;
490         }
491
492         /* Initialize the RESET line. */
493
494         if (pdata->gpio_reset != UCMB_NO_RESET) {
495                 err = gpio_request(pdata->gpio_reset, pdata->name);
496                 if (err) {
497                         printk(KERN_ERR PFX
498                                "Failed to request RESET GPIO line\n");
499                         goto err_unreg_spi_device;
500                 }
501                 err = gpio_direction_output(pdata->gpio_reset,
502                                             pdata->reset_activelow);
503                 if (err) {
504                         printk(KERN_ERR PFX
505                                "Failed to set RESET GPIO direction\n");
506                         goto err_free_reset_gpio;
507                 }
508                 ucmb_reset_microcontroller(ucmb);
509         }
510
511         /* Create the Misc char device. */
512
513         ucmb->mdev.minor = MISC_DYNAMIC_MINOR;
514         ucmb->mdev.name = pdata->name;
515         ucmb->mdev.parent = &pdev->dev;
516         ucmb->mdev_fops.open = ucmb_open;
517         ucmb->mdev_fops.release = ucmb_release;
518         ucmb->mdev_fops.read = ucmb_read;
519         ucmb->mdev_fops.write = ucmb_write;
520         ucmb->mdev_fops.ioctl = ucmb_ioctl;
521         ucmb->mdev.fops = &ucmb->mdev_fops;
522
523         err = misc_register(&ucmb->mdev);
524         if (err) {
525                 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
526                        ucmb->mdev.name);
527                 goto err_free_reset_gpio;
528         }
529
530         platform_set_drvdata(pdev, ucmb);
531
532         printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
533
534         return 0;
535
536 err_free_reset_gpio:
537         if (pdata->gpio_reset != UCMB_NO_RESET)
538                 gpio_free(pdata->gpio_reset);
539 err_unreg_spi_device:
540         spi_unregister_device(ucmb->sdev);
541 err_free_spi_device:
542         spi_dev_put(ucmb->sdev);
543 err_unreg_spi_gpio_pdev:
544         platform_device_unregister(&ucmb->spi_gpio_pdev);
545 err_free_ucmb:
546         kfree(ucmb);
547
548         return err;
549 }
550
551 static int __devexit ucmb_remove(struct platform_device *pdev)
552 {
553         struct ucmb *ucmb = platform_get_drvdata(pdev);
554         int err;
555
556         err = misc_deregister(&ucmb->mdev);
557         if (err) {
558                 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
559                        ucmb->mdev.name);
560         }
561         if (ucmb->gpio_reset != UCMB_NO_RESET)
562                 gpio_free(ucmb->gpio_reset);
563         spi_unregister_device(ucmb->sdev);
564         spi_dev_put(ucmb->sdev);
565         platform_device_unregister(&ucmb->spi_gpio_pdev);
566
567         kfree(ucmb);
568         platform_set_drvdata(pdev, NULL);
569
570         return 0;
571 }
572
573 static struct platform_driver ucmb_driver = {
574         .driver         = {
575                 .name   = "ucmb",
576                 .owner  = THIS_MODULE,
577         },
578         .probe          = ucmb_probe,
579         .remove         = __devexit_p(ucmb_remove),
580 };
581
582 int ucmb_device_register(struct ucmb_platform_data *pdata)
583 {
584         struct platform_device *pdev;
585         int err;
586
587         pdev = platform_device_alloc("ucmb", ucmb_pdev_id_count++);
588         if (!pdev) {
589                 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
590                 return -ENOMEM;
591         }
592         err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
593         if (err) {
594                 printk(KERN_ERR PFX "Failed to add platform data.\n");
595                 platform_device_put(pdev);
596                 return err;
597         }
598         err = platform_device_add(pdev);
599         if (err) {
600                 printk(KERN_ERR PFX "Failed to register platform device.\n");
601                 platform_device_put(pdev);
602                 return err;
603         }
604         pdata->pdev = pdev;
605
606         return 0;
607 }
608 EXPORT_SYMBOL(ucmb_device_register);
609
610 void ucmb_device_unregister(struct ucmb_platform_data *pdata)
611 {
612         if (!pdata->pdev)
613                 return;
614         platform_device_unregister(pdata->pdev);
615         platform_device_put(pdata->pdev);
616         pdata->pdev = NULL;
617 }
618 EXPORT_SYMBOL(ucmb_device_unregister);
619
620 static int ucmb_modinit(void)
621 {
622         int err;
623
624         printk(KERN_INFO "Microcontroller message bus driver\n");
625
626         err = spi_register_driver(&ucmb_spi_driver);
627         if (err) {
628                 printk(KERN_ERR PFX "Failed to register SPI driver\n");
629                 return err;
630         }
631         err = platform_driver_register(&ucmb_driver);
632         if (err) {
633                 printk(KERN_ERR PFX "Failed to register platform driver\n");
634                 spi_unregister_driver(&ucmb_spi_driver);
635                 return err;
636         }
637
638         return 0;
639 }
640 subsys_initcall(ucmb_modinit);
641
642 static void ucmb_modexit(void)
643 {
644         platform_driver_unregister(&ucmb_driver);
645         spi_unregister_driver(&ucmb_spi_driver);
646 }
647 module_exit(ucmb_modexit);