2 * Microcontroller Message Bus
4 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
6 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/miscdevice.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_gpio.h>
17 #include <linux/spi/spi_bitbang.h>
18 #include <linux/gfp.h>
19 #include <linux/delay.h>
20 #include <linux/crc16.h>
28 MODULE_LICENSE("GPL");
29 MODULE_DESCRIPTION("Microcontroller Message Bus");
30 MODULE_AUTHOR("Michael Buesch");
34 unsigned int msg_delay_ms
;
36 /* Misc character device driver */
37 struct miscdevice mdev
;
38 struct file_operations mdev_fops
;
41 struct spi_device
*sdev
;
44 struct spi_gpio_platform_data spi_gpio_pdata
;
45 struct platform_device spi_gpio_pdev
;
48 struct ucmb_message_hdr
{
49 __le16 magic
; /* UCMB_MAGIC */
50 __le16 len
; /* Payload length (excluding header) */
51 } __attribute__((packed
));
53 struct ucmb_message_footer
{
54 __le16 crc
; /* CRC of the header + payload. */
55 } __attribute__((packed
));
58 __le16 magic
; /* UCMB_MAGIC */
59 __le16 code
; /* enum ucmb_status_code */
60 } __attribute__((packed
));
62 #define UCMB_MAGIC 0x1337
63 #define UCMB_MAX_MSG_LEN 0x200
65 enum ucmb_status_code
{
67 UCMB_STAT_EPROTO
, /* Protocol format error */
68 UCMB_STAT_ENOMEM
, /* Out of memory */
69 UCMB_STAT_E2BIG
, /* Message too big */
70 UCMB_STAT_ECRC
, /* CRC error */
74 static int ucmb_spi_busnum_count
= 1337;
77 static struct ucmb_platform_data ucmb_list
[] = {
78 { //FIXME don't define it here.
85 .max_speed_hz
= 128000, /* Hz */
86 .msg_delay_ms
= 1, /* mS */
91 static int __devinit
ucmb_spi_probe(struct spi_device
*sdev
)
96 static int __devexit
ucmb_spi_remove(struct spi_device
*sdev
)
101 static struct spi_driver ucmb_spi_driver
= {
104 .bus
= &spi_bus_type
,
105 .owner
= THIS_MODULE
,
107 .probe
= ucmb_spi_probe
,
108 .remove
= __devexit_p(ucmb_spi_remove
),
111 static int ucmb_status_code_to_errno(enum ucmb_status_code code
)
116 case UCMB_STAT_EPROTO
:
118 case UCMB_STAT_ENOMEM
:
120 case UCMB_STAT_E2BIG
:
128 static inline struct ucmb
* filp_to_ucmb(struct file
*filp
)
130 return container_of(filp
->f_op
, struct ucmb
, mdev_fops
);
133 static ssize_t
ucmb_read(struct file
*filp
, char __user
*user_buf
,
134 size_t size
, loff_t
*offp
)
136 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
139 struct ucmb_message_hdr hdr
;
140 struct ucmb_message_footer footer
;
141 struct ucmb_status status
= { .magic
= cpu_to_le16(UCMB_MAGIC
), };
144 size
= min_t(size_t, size
, PAGE_SIZE
);
147 buf
= (char *)__get_free_page(GFP_KERNEL
);
151 err
= spi_read(ucmb
->sdev
, (u8
*)&hdr
, sizeof(hdr
));
155 printk(KERN_DEBUG PFX
"Received message header 0x%04X 0x%04X\n",
156 le16_to_cpu(hdr
.magic
), le16_to_cpu(hdr
.len
));
159 if (hdr
.magic
!= cpu_to_le16(UCMB_MAGIC
))
162 if (size
< le16_to_cpu(hdr
.len
))
164 size
= le16_to_cpu(hdr
.len
);
165 err
= spi_read(ucmb
->sdev
, buf
, size
);
168 err
= spi_read(ucmb
->sdev
, (u8
*)&footer
, sizeof(footer
));
172 crc
= crc16(crc
, &hdr
, sizeof(hdr
));
173 crc
= crc16(crc
, buf
, size
);
175 if (crc
!= le16_to_cpu(footer
.crc
)) {
177 status
.code
= UCMB_STAT_ECRC
;
178 goto out_send_status
;
181 if (copy_to_user(user_buf
, buf
, size
)) {
183 status
.code
= UCMB_STAT_ENOMEM
;
184 goto out_send_status
;
187 status
.code
= UCMB_STAT_OK
;
191 res
= spi_write(ucmb
->sdev
, (u8
*)&status
, sizeof(status
));
195 free_page((unsigned long)buf
);
197 return err
? err
: size
;
200 static ssize_t
ucmb_write(struct file
*filp
, const char __user
*user_buf
,
201 size_t size
, loff_t
*offp
)
203 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
206 struct ucmb_message_hdr hdr
= { .magic
= cpu_to_le16(UCMB_MAGIC
), };
207 struct ucmb_message_footer footer
= { .crc
= 0xFFFF, };
208 struct ucmb_status status
;
209 struct spi_transfer spi_hdr_xfer
;
210 struct spi_transfer spi_footer_xfer
;
211 struct spi_transfer spi_data_xfer
;
212 struct spi_message spi_msg
;
215 buf
= (char *)__get_free_page(GFP_KERNEL
);
219 size
= min_t(size_t, PAGE_SIZE
, size
);
220 size
= min_t(size_t, UCMB_MAX_MSG_LEN
, size
);
222 if (copy_from_user(buf
, user_buf
, size
))
224 hdr
.len
= cpu_to_le16(size
);
226 footer
.crc
= crc16(footer
.crc
, &hdr
, sizeof(hdr
));
227 footer
.crc
= crc16(footer
.crc
, buf
, size
);
228 footer
.crc
^= 0xFFFF;
230 spi_message_init(&spi_msg
);
232 memset(&spi_hdr_xfer
, 0, sizeof(spi_hdr_xfer
));
233 spi_hdr_xfer
.tx_buf
= &hdr
;
234 spi_hdr_xfer
.len
= sizeof(hdr
);
235 spi_message_add_tail(&spi_hdr_xfer
, &spi_msg
);
237 memset(&spi_data_xfer
, 0, sizeof(spi_data_xfer
));
238 spi_data_xfer
.tx_buf
= buf
;
239 spi_data_xfer
.len
= size
;
240 spi_message_add_tail(&spi_data_xfer
, &spi_msg
);
242 memset(&spi_footer_xfer
, 0, sizeof(spi_footer_xfer
));
243 spi_footer_xfer
.tx_buf
= &footer
;
244 spi_footer_xfer
.len
= sizeof(footer
);
245 spi_message_add_tail(&spi_footer_xfer
, &spi_msg
);
247 /* Send the message, including header. */
248 err
= spi_sync(ucmb
->sdev
, &spi_msg
);
252 /* The microcontroller deserves some time to process the message. */
253 if (ucmb
->msg_delay_ms
)
254 msleep(ucmb
->msg_delay_ms
);
256 /* Get the status code. */
257 err
= spi_read(ucmb
->sdev
, (u8
*)&status
, sizeof(status
));
261 printk(KERN_DEBUG PFX
"Sent message. Status report: 0x%04X 0x%04X\n",
262 le16_to_cpu(status
.magic
), le16_to_cpu(status
.code
));
265 if (status
.magic
!= cpu_to_le16(UCMB_MAGIC
))
267 err
= ucmb_status_code_to_errno(le16_to_cpu(status
.code
));
272 free_page((unsigned long)buf
);
274 return err
? err
: size
;
277 static int __devinit
ucmb_probe(struct platform_device
*pdev
)
279 struct ucmb_platform_data
*pdata
;
282 const int bus_num
= ucmb_spi_busnum_count
++;
283 struct spi_bitbang
*bb
;
285 pdata
= pdev
->dev
.platform_data
;
289 ucmb
= kzalloc(sizeof(struct ucmb
), GFP_KERNEL
);
292 ucmb
->msg_delay_ms
= pdata
->msg_delay_ms
;
294 /* Create the SPI GPIO bus master. */
296 #ifdef CONFIG_SPI_GPIO_MODULE
297 err
= request_module("spi_gpio");
299 printk(KERN_WARNING PFX
"Failed to request spi_gpio module\n");
300 #endif /* CONFIG_SPI_GPIO_MODULE */
302 ucmb
->spi_gpio_pdata
.sck
= pdata
->gpio_sck
;
303 ucmb
->spi_gpio_pdata
.mosi
= pdata
->gpio_mosi
;
304 ucmb
->spi_gpio_pdata
.miso
= pdata
->gpio_miso
;
305 ucmb
->spi_gpio_pdata
.num_chipselect
= 1;
307 ucmb
->spi_gpio_pdev
.name
= "spi_gpio";
308 ucmb
->spi_gpio_pdev
.id
= bus_num
;
309 ucmb
->spi_gpio_pdev
.dev
.platform_data
= &ucmb
->spi_gpio_pdata
;
311 err
= platform_device_register(&ucmb
->spi_gpio_pdev
);
313 printk(KERN_ERR PFX
"Failed to register SPI-GPIO platform device\n");
316 bb
= platform_get_drvdata(&ucmb
->spi_gpio_pdev
);
317 if (!bb
|| !bb
->master
) {
318 printk(KERN_ERR PFX
"No bitbanged master device found.\n");
319 goto err_unreg_spi_gpio_pdev
;
322 /* Create the SPI device. */
324 ucmb
->sdev
= spi_alloc_device(bb
->master
);
326 printk(KERN_ERR PFX
"Failed to allocate SPI device\n");
327 goto err_unreg_spi_gpio_pdev
;
329 ucmb
->sdev
->max_speed_hz
= pdata
->max_speed_hz
;
330 ucmb
->sdev
->chip_select
= 0;
331 ucmb
->sdev
->mode
= pdata
->mode
;
332 strlcpy(ucmb
->sdev
->modalias
, "ucmb", /* We are the SPI driver. */
333 sizeof(ucmb
->sdev
->modalias
));
334 ucmb
->sdev
->controller_data
= (void *)pdata
->gpio_cs
;
335 err
= spi_add_device(ucmb
->sdev
);
337 printk(KERN_ERR PFX
"Failed to add SPI device\n");
338 goto err_free_spi_device
;
341 /* Create the Misc char device. */
343 ucmb
->mdev
.minor
= MISC_DYNAMIC_MINOR
;
344 ucmb
->mdev
.name
= pdata
->name
;
345 ucmb
->mdev
.parent
= &pdev
->dev
;
346 ucmb
->mdev_fops
.read
= ucmb_read
;
347 ucmb
->mdev_fops
.write
= ucmb_write
;
348 ucmb
->mdev
.fops
= &ucmb
->mdev_fops
;
350 err
= misc_register(&ucmb
->mdev
);
352 printk(KERN_ERR PFX
"Failed to register miscdev %s\n",
354 goto err_unreg_spi_device
;
357 platform_set_drvdata(pdev
, ucmb
);
359 printk(KERN_INFO PFX
"Registered message bus \"%s\"\n", pdata
->name
);
363 err_unreg_spi_device
:
364 spi_unregister_device(ucmb
->sdev
);
366 spi_dev_put(ucmb
->sdev
);
367 err_unreg_spi_gpio_pdev
:
368 platform_device_unregister(&ucmb
->spi_gpio_pdev
);
375 static int __devexit
ucmb_remove(struct platform_device
*pdev
)
377 struct ucmb
*ucmb
= platform_get_drvdata(pdev
);
380 err
= misc_deregister(&ucmb
->mdev
);
382 printk(KERN_ERR PFX
"Failed to unregister miscdev %s\n",
385 spi_unregister_device(ucmb
->sdev
);
386 spi_dev_put(ucmb
->sdev
);
387 platform_device_unregister(&ucmb
->spi_gpio_pdev
);
390 platform_set_drvdata(pdev
, NULL
);
395 static struct platform_driver ucmb_driver
= {
398 .owner
= THIS_MODULE
,
401 .remove
= __devexit_p(ucmb_probe
),
404 static int ucmb_modinit(void)
406 struct ucmb_platform_data
*pdata
;
407 struct platform_device
*pdev
;
410 printk(KERN_INFO
"Microcontroller message bus driver\n");
412 err
= platform_driver_register(&ucmb_driver
);
414 printk(KERN_ERR PFX
"Failed to register platform driver\n");
417 err
= spi_register_driver(&ucmb_spi_driver
);
419 printk(KERN_ERR PFX
"Failed to register SPI driver\n");
420 platform_driver_unregister(&ucmb_driver
);
424 for (i
= 0; i
< ARRAY_SIZE(ucmb_list
); i
++) {
425 pdata
= &ucmb_list
[i
];
427 pdev
= platform_device_alloc("ucmb", i
);
429 printk(KERN_ERR PFX
"Failed to allocate platform device.\n");
432 err
= platform_device_add_data(pdev
, pdata
, sizeof(*pdata
));
434 printk(KERN_ERR PFX
"Failed to add platform data.\n");
435 platform_device_put(pdev
);
438 err
= platform_device_add(pdev
);
440 printk(KERN_ERR PFX
"Failed to register platform device.\n");
441 platform_device_put(pdev
);
449 module_init(ucmb_modinit
);
451 static void ucmb_modexit(void)
453 struct ucmb_platform_data
*pdata
;
456 for (i
= 0; i
< ARRAY_SIZE(ucmb_list
); i
++) {
457 pdata
= &ucmb_list
[i
];
460 platform_device_unregister(pdata
->pdev
);
461 platform_device_put(pdata
->pdev
);
464 spi_unregister_driver(&ucmb_spi_driver
);
465 platform_driver_unregister(&ucmb_driver
);
467 module_exit(ucmb_modexit
);