2 * Microcontroller Message Bus
5 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
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.
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.
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/miscdevice.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>
33 #include <asm/uaccess.h>
41 MODULE_LICENSE("GPL");
42 MODULE_DESCRIPTION("Microcontroller Message Bus");
43 MODULE_AUTHOR("Michael Buesch");
51 unsigned int chunk_size;
52 unsigned int msg_delay_usec;
53 unsigned int gpio_reset;
56 /* Misc character device driver */
57 struct miscdevice mdev;
58 struct file_operations mdev_fops;
61 struct spi_device *sdev;
64 struct spi_gpio_platform_data spi_gpio_pdata;
65 struct platform_device spi_gpio_pdev;
68 #define UCMB_MAX_MSG_DELAY (10 * 1000 * 1000) /* 10 seconds */
71 struct ucmb_message_hdr {
72 __le16 magic; /* UCMB_MAGIC */
73 __le16 len; /* Payload length (excluding header and footer) */
74 } __attribute__((packed));
76 struct ucmb_message_footer {
77 __le16 crc; /* CRC of the header + payload. */
78 } __attribute__((packed));
81 __le16 magic; /* UCMB_MAGIC */
82 __le16 code; /* enum ucmb_status_code */
83 } __attribute__((packed));
85 #define UCMB_MAGIC 0x1337
87 enum ucmb_status_code {
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 */
96 static int ucmb_spi_busnum_count = 1337;
97 static int ucmb_pdev_id_count;
100 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
105 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
110 static struct spi_driver ucmb_spi_driver = {
113 .bus = &spi_bus_type,
114 .owner = THIS_MODULE,
116 .probe = ucmb_spi_probe,
117 .remove = __devexit_p(ucmb_spi_remove),
120 static void ucmb_toggle_reset_line(struct ucmb *ucmb, bool active)
122 if (ucmb->reset_activelow)
124 gpio_set_value(ucmb->gpio_reset, active);
127 static int ucmb_reset_microcontroller(struct ucmb *ucmb)
129 if (ucmb->gpio_reset == UCMB_NO_RESET)
132 ucmb_toggle_reset_line(ucmb, 1);
134 ucmb_toggle_reset_line(ucmb, 0);
140 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
145 case UCMB_STAT_EPROTO:
147 case UCMB_STAT_ENOMEM:
149 case UCMB_STAT_E2BIG:
157 static inline struct ucmb * filp_to_ucmb(struct file *filp)
159 return container_of(filp->f_op, struct ucmb, mdev_fops);
162 static int ucmb_open(struct inode *inode, struct file *filp)
164 struct ucmb *ucmb = filp_to_ucmb(filp);
167 mutex_lock(&ucmb->mutex);
174 ucmb->msg_delay_usec = 0;
177 mutex_unlock(&ucmb->mutex);
182 static int ucmb_release(struct inode *inode, struct file *filp)
184 struct ucmb *ucmb = filp_to_ucmb(filp);
186 mutex_lock(&ucmb->mutex);
187 WARN_ON(!ucmb->is_open);
189 mutex_unlock(&ucmb->mutex);
194 static int ucmb_ioctl(struct inode *inode, struct file *filp,
195 unsigned int cmd, unsigned long arg)
197 struct ucmb *ucmb = filp_to_ucmb(filp);
200 mutex_lock(&ucmb->mutex);
202 case UCMB_IOCTL_RESETUC:
203 ret = ucmb_reset_microcontroller(ucmb);
205 case UCMB_IOCTL_GMSGDELAY:
206 if (put_user(ucmb->msg_delay_usec, (unsigned int __user *)arg)) {
211 case UCMB_IOCTL_SMSGDELAY: {
212 unsigned int msg_delay_usec;
214 if (get_user(msg_delay_usec, (unsigned int __user *)arg)) {
218 if (msg_delay_usec > UCMB_MAX_MSG_DELAY) {
222 ucmb->msg_delay_usec = msg_delay_usec;
228 mutex_unlock(&ucmb->mutex);
233 static int ucmb_spi_write(struct ucmb *ucmb, const void *_buf, size_t size)
235 const u8 *buf = _buf;
236 size_t i, chunk_size, current_size;
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);
247 if (ucmb->chunk_size && need_resched())
254 static int ucmb_spi_read(struct ucmb *ucmb, void *_buf, size_t size)
257 size_t i, chunk_size, current_size;
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);
268 if (ucmb->chunk_size && need_resched())
275 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
276 size_t size, loff_t *offp)
278 struct ucmb *ucmb = filp_to_ucmb(filp);
281 struct ucmb_message_hdr hdr;
282 struct ucmb_message_footer footer;
283 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
286 mutex_lock(&ucmb->mutex);
288 size = min_t(size_t, size, PAGE_SIZE);
291 buf = (char *)__get_free_page(GFP_KERNEL);
295 err = ucmb_spi_read(ucmb, &hdr, sizeof(hdr));
299 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
300 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
303 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
306 if (size < le16_to_cpu(hdr.len))
308 size = le16_to_cpu(hdr.len);
309 err = ucmb_spi_read(ucmb, buf, size);
312 err = ucmb_spi_read(ucmb, &footer, sizeof(footer));
316 crc = crc16(crc, (u8 *)&hdr, sizeof(hdr));
317 crc = crc16(crc, buf, size);
319 if (crc != le16_to_cpu(footer.crc)) {
321 status.code = UCMB_STAT_ECRC;
322 goto out_send_status;
325 if (copy_to_user(user_buf, buf, size)) {
327 status.code = UCMB_STAT_ENOMEM;
328 goto out_send_status;
331 status.code = UCMB_STAT_OK;
335 res = ucmb_spi_write(ucmb, &status, sizeof(status));
339 free_page((unsigned long)buf);
341 mutex_unlock(&ucmb->mutex);
343 return err ? err : size;
346 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
347 size_t size, loff_t *offp)
349 struct ucmb *ucmb = filp_to_ucmb(filp);
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;
357 mutex_lock(&ucmb->mutex);
360 buf = (char *)__get_free_page(GFP_KERNEL);
364 size = min_t(size_t, PAGE_SIZE, size);
366 if (copy_from_user(buf, user_buf, size))
368 hdr.len = cpu_to_le16(size);
370 footer.crc = crc16(footer.crc, (u8 *)&hdr, sizeof(hdr));
371 footer.crc = crc16(footer.crc, buf, size);
372 footer.crc ^= 0xFFFF;
374 err = ucmb_spi_write(ucmb, &hdr, sizeof(hdr));
377 err = ucmb_spi_write(ucmb, buf, size);
380 err = ucmb_spi_write(ucmb, &footer, sizeof(footer));
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));
392 udelay(ucmb->msg_delay_usec);
395 /* Get the status code. */
396 err = ucmb_spi_read(ucmb, &status, sizeof(status));
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));
404 if (status.magic != cpu_to_le16(UCMB_MAGIC))
406 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
411 free_page((unsigned long)buf);
413 mutex_unlock(&ucmb->mutex);
415 return err ? err : size;
418 static int __devinit ucmb_probe(struct platform_device *pdev)
420 struct ucmb_platform_data *pdata;
423 const int bus_num = ucmb_spi_busnum_count++;
424 struct spi_bitbang *bb;
426 pdata = pdev->dev.platform_data;
430 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
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;
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;
445 /* Create the SPI GPIO bus master. */
447 #ifdef CONFIG_SPI_GPIO_MODULE
448 err = request_module("spi_gpio");
450 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
451 #endif /* CONFIG_SPI_GPIO_MODULE */
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;
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;
462 err = platform_device_register(&ucmb->spi_gpio_pdev);
464 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
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;
473 /* Create the SPI device. */
475 ucmb->sdev = spi_alloc_device(bb->master);
477 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
478 goto err_unreg_spi_gpio_pdev;
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);
488 printk(KERN_ERR PFX "Failed to add SPI device\n");
489 goto err_free_spi_device;
492 /* Initialize the RESET line. */
494 if (pdata->gpio_reset != UCMB_NO_RESET) {
495 err = gpio_request(pdata->gpio_reset, pdata->name);
498 "Failed to request RESET GPIO line\n");
499 goto err_unreg_spi_device;
501 err = gpio_direction_output(pdata->gpio_reset,
502 pdata->reset_activelow);
505 "Failed to set RESET GPIO direction\n");
506 goto err_free_reset_gpio;
508 ucmb_reset_microcontroller(ucmb);
511 /* Create the Misc char device. */
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;
523 err = misc_register(&ucmb->mdev);
525 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
527 goto err_free_reset_gpio;
530 platform_set_drvdata(pdev, ucmb);
532 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
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);
542 spi_dev_put(ucmb->sdev);
543 err_unreg_spi_gpio_pdev:
544 platform_device_unregister(&ucmb->spi_gpio_pdev);
551 static int __devexit ucmb_remove(struct platform_device *pdev)
553 struct ucmb *ucmb = platform_get_drvdata(pdev);
556 err = misc_deregister(&ucmb->mdev);
558 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
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);
568 platform_set_drvdata(pdev, NULL);
573 static struct platform_driver ucmb_driver = {
576 .owner = THIS_MODULE,
579 .remove = __devexit_p(ucmb_remove),
582 int ucmb_device_register(struct ucmb_platform_data *pdata)
584 struct platform_device *pdev;
587 pdev = platform_device_alloc("ucmb", ucmb_pdev_id_count++);
589 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
592 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
594 printk(KERN_ERR PFX "Failed to add platform data.\n");
595 platform_device_put(pdev);
598 err = platform_device_add(pdev);
600 printk(KERN_ERR PFX "Failed to register platform device.\n");
601 platform_device_put(pdev);
608 EXPORT_SYMBOL(ucmb_device_register);
610 void ucmb_device_unregister(struct ucmb_platform_data *pdata)
614 platform_device_unregister(pdata->pdev);
615 platform_device_put(pdata->pdev);
618 EXPORT_SYMBOL(ucmb_device_unregister);
620 static int ucmb_modinit(void)
624 printk(KERN_INFO "Microcontroller message bus driver\n");
626 err = spi_register_driver(&ucmb_spi_driver);
628 printk(KERN_ERR PFX "Failed to register SPI driver\n");
631 err = platform_driver_register(&ucmb_driver);
633 printk(KERN_ERR PFX "Failed to register platform driver\n");
634 spi_unregister_driver(&ucmb_spi_driver);
640 subsys_initcall(ucmb_modinit);
642 static void ucmb_modexit(void)
644 platform_driver_unregister(&ucmb_driver);
645 spi_unregister_driver(&ucmb_spi_driver);
647 module_exit(ucmb_modexit);