ucmb: Add more documentation and example code for AVR8
[openwrt/svn-archive/archive.git] / utils / ucmb / driver / ucmb.c
1 /*
2 * Microcontroller Message Bus
3 *
4 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
5 *
6 * Licensed under the GNU/GPL. See COPYING for details.
7 */
8
9 #include "ucmb.h"
10
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/miscdevice.h>
14 #include <linux/fs.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>
21
22
23 #define PFX "ucmb: "
24
25 #undef DEBUG
26
27
28 MODULE_LICENSE("GPL");
29 MODULE_DESCRIPTION("Microcontroller Message Bus");
30 MODULE_AUTHOR("Michael Buesch");
31
32
33 struct ucmb {
34 unsigned int msg_delay_ms;
35
36 /* Misc character device driver */
37 struct miscdevice mdev;
38 struct file_operations mdev_fops;
39
40 /* SPI driver */
41 struct spi_device *sdev;
42
43 /* SPI-GPIO driver */
44 struct spi_gpio_platform_data spi_gpio_pdata;
45 struct platform_device spi_gpio_pdev;
46 };
47
48 struct ucmb_message_hdr {
49 __le16 magic; /* UCMB_MAGIC */
50 __le16 len; /* Payload length (excluding header) */
51 } __attribute__((packed));
52
53 struct ucmb_message_footer {
54 __le16 crc; /* CRC of the header + payload. */
55 } __attribute__((packed));
56
57 struct ucmb_status {
58 __le16 magic; /* UCMB_MAGIC */
59 __le16 code; /* enum ucmb_status_code */
60 } __attribute__((packed));
61
62 #define UCMB_MAGIC 0x1337
63
64 enum ucmb_status_code {
65 UCMB_STAT_OK = 0,
66 UCMB_STAT_EPROTO, /* Protocol format error */
67 UCMB_STAT_ENOMEM, /* Out of memory */
68 UCMB_STAT_E2BIG, /* Message too big */
69 UCMB_STAT_ECRC, /* CRC error */
70 };
71
72
73 static int ucmb_spi_busnum_count = 1337;
74
75
76 static struct ucmb_platform_data ucmb_list[] = {
77 { //FIXME don't define it here.
78 .name = "ucmb",
79 .gpio_cs = 3,
80 .gpio_sck = 0,
81 .gpio_miso = 1,
82 .gpio_mosi = 2,
83 .mode = SPI_MODE_0,
84 .max_speed_hz = 128000, /* Hz */
85 .msg_delay_ms = 1, /* mS */
86 },
87 };
88
89
90 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
91 {
92 return 0;
93 }
94
95 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
96 {
97 return 0;
98 }
99
100 static struct spi_driver ucmb_spi_driver = {
101 .driver = {
102 .name = "ucmb",
103 .bus = &spi_bus_type,
104 .owner = THIS_MODULE,
105 },
106 .probe = ucmb_spi_probe,
107 .remove = __devexit_p(ucmb_spi_remove),
108 };
109
110 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
111 {
112 switch (code) {
113 case UCMB_STAT_OK:
114 return 0;
115 case UCMB_STAT_EPROTO:
116 return -EPROTO;
117 case UCMB_STAT_ENOMEM:
118 return -ENOMEM;
119 case UCMB_STAT_E2BIG:
120 return -E2BIG;
121 case UCMB_STAT_ECRC:
122 return -EBADMSG;
123 }
124 return -EBUSY;
125 }
126
127 static inline struct ucmb * filp_to_ucmb(struct file *filp)
128 {
129 return container_of(filp->f_op, struct ucmb, mdev_fops);
130 }
131
132 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
133 size_t size, loff_t *offp)
134 {
135 struct ucmb *ucmb = filp_to_ucmb(filp);
136 u8 *buf;
137 int res, err;
138 struct ucmb_message_hdr hdr;
139 struct ucmb_message_footer footer;
140 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
141 u16 crc = 0xFFFF;
142
143 size = min_t(size_t, size, PAGE_SIZE);
144
145 err = -ENOMEM;
146 buf = (char *)__get_free_page(GFP_KERNEL);
147 if (!buf)
148 goto out;
149
150 err = spi_read(ucmb->sdev, (u8 *)&hdr, sizeof(hdr));
151 if (err)
152 goto out_free;
153 #ifdef DEBUG
154 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
155 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
156 #endif
157 err = -EPROTO;
158 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
159 goto out_free;
160 err = -ENOBUFS;
161 if (size < le16_to_cpu(hdr.len))
162 goto out_free;
163 size = le16_to_cpu(hdr.len);
164 err = spi_read(ucmb->sdev, buf, size);
165 if (err)
166 goto out_free;
167 err = spi_read(ucmb->sdev, (u8 *)&footer, sizeof(footer));
168 if (err)
169 goto out_free;
170
171 crc = crc16(crc, &hdr, sizeof(hdr));
172 crc = crc16(crc, buf, size);
173 crc ^= 0xFFFF;
174 if (crc != le16_to_cpu(footer.crc)) {
175 err = -EPROTO;
176 status.code = UCMB_STAT_ECRC;
177 goto out_send_status;
178 }
179
180 if (copy_to_user(user_buf, buf, size)) {
181 err = -EFAULT;
182 status.code = UCMB_STAT_ENOMEM;
183 goto out_send_status;
184 }
185
186 status.code = UCMB_STAT_OK;
187 err = 0;
188
189 out_send_status:
190 res = spi_write(ucmb->sdev, (u8 *)&status, sizeof(status));
191 if (res && !err)
192 err = res;
193 out_free:
194 free_page((unsigned long)buf);
195 out:
196 return err ? err : size;
197 }
198
199 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
200 size_t size, loff_t *offp)
201 {
202 struct ucmb *ucmb = filp_to_ucmb(filp);
203 u8 *buf;
204 int err;
205 struct ucmb_message_hdr hdr = { .magic = cpu_to_le16(UCMB_MAGIC), };
206 struct ucmb_message_footer footer = { .crc = 0xFFFF, };
207 struct ucmb_status status;
208 struct spi_transfer spi_hdr_xfer;
209 struct spi_transfer spi_footer_xfer;
210 struct spi_transfer spi_data_xfer;
211 struct spi_message spi_msg;
212
213 err = -ENOMEM;
214 buf = (char *)__get_free_page(GFP_KERNEL);
215 if (!buf)
216 goto out;
217
218 size = min_t(size_t, PAGE_SIZE, size);
219 err = -EFAULT;
220 if (copy_from_user(buf, user_buf, size))
221 goto out_free;
222 hdr.len = cpu_to_le16(size);
223
224 footer.crc = crc16(footer.crc, &hdr, sizeof(hdr));
225 footer.crc = crc16(footer.crc, buf, size);
226 footer.crc ^= 0xFFFF;
227
228 spi_message_init(&spi_msg);
229
230 memset(&spi_hdr_xfer, 0, sizeof(spi_hdr_xfer));
231 spi_hdr_xfer.tx_buf = &hdr;
232 spi_hdr_xfer.len = sizeof(hdr);
233 spi_message_add_tail(&spi_hdr_xfer, &spi_msg);
234
235 memset(&spi_data_xfer, 0, sizeof(spi_data_xfer));
236 spi_data_xfer.tx_buf = buf;
237 spi_data_xfer.len = size;
238 spi_message_add_tail(&spi_data_xfer, &spi_msg);
239
240 memset(&spi_footer_xfer, 0, sizeof(spi_footer_xfer));
241 spi_footer_xfer.tx_buf = &footer;
242 spi_footer_xfer.len = sizeof(footer);
243 spi_message_add_tail(&spi_footer_xfer, &spi_msg);
244
245 /* Send the message, including header. */
246 err = spi_sync(ucmb->sdev, &spi_msg);
247 if (err)
248 goto out_free;
249
250 /* The microcontroller deserves some time to process the message. */
251 if (ucmb->msg_delay_ms)
252 msleep(ucmb->msg_delay_ms);
253
254 /* Get the status code. */
255 err = spi_read(ucmb->sdev, (u8 *)&status, sizeof(status));
256 if (err)
257 goto out_free;
258 #ifdef DEBUG
259 printk(KERN_DEBUG PFX "Sent message. Status report: 0x%04X 0x%04X\n",
260 le16_to_cpu(status.magic), le16_to_cpu(status.code));
261 #endif
262 err = -EPROTO;
263 if (status.magic != cpu_to_le16(UCMB_MAGIC))
264 goto out_free;
265 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
266 if (err)
267 goto out_free;
268
269 out_free:
270 free_page((unsigned long)buf);
271 out:
272 return err ? err : size;
273 }
274
275 static int __devinit ucmb_probe(struct platform_device *pdev)
276 {
277 struct ucmb_platform_data *pdata;
278 struct ucmb *ucmb;
279 int err;
280 const int bus_num = ucmb_spi_busnum_count++;
281 struct spi_bitbang *bb;
282
283 pdata = pdev->dev.platform_data;
284 if (!pdata)
285 return -ENXIO;
286
287 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
288 if (!ucmb)
289 return -ENOMEM;
290 ucmb->msg_delay_ms = pdata->msg_delay_ms;
291
292 /* Create the SPI GPIO bus master. */
293
294 #ifdef CONFIG_SPI_GPIO_MODULE
295 err = request_module("spi_gpio");
296 if (err)
297 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
298 #endif /* CONFIG_SPI_GPIO_MODULE */
299
300 ucmb->spi_gpio_pdata.sck = pdata->gpio_sck;
301 ucmb->spi_gpio_pdata.mosi = pdata->gpio_mosi;
302 ucmb->spi_gpio_pdata.miso = pdata->gpio_miso;
303 ucmb->spi_gpio_pdata.num_chipselect = 1;
304
305 ucmb->spi_gpio_pdev.name = "spi_gpio";
306 ucmb->spi_gpio_pdev.id = bus_num;
307 ucmb->spi_gpio_pdev.dev.platform_data = &ucmb->spi_gpio_pdata;
308
309 err = platform_device_register(&ucmb->spi_gpio_pdev);
310 if (err) {
311 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
312 goto err_free_ucmb;
313 }
314 bb = platform_get_drvdata(&ucmb->spi_gpio_pdev);
315 if (!bb || !bb->master) {
316 printk(KERN_ERR PFX "No bitbanged master device found.\n");
317 goto err_unreg_spi_gpio_pdev;
318 }
319
320 /* Create the SPI device. */
321
322 ucmb->sdev = spi_alloc_device(bb->master);
323 if (!ucmb->sdev) {
324 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
325 goto err_unreg_spi_gpio_pdev;
326 }
327 ucmb->sdev->max_speed_hz = pdata->max_speed_hz;
328 ucmb->sdev->chip_select = 0;
329 ucmb->sdev->mode = pdata->mode;
330 strlcpy(ucmb->sdev->modalias, "ucmb", /* We are the SPI driver. */
331 sizeof(ucmb->sdev->modalias));
332 ucmb->sdev->controller_data = (void *)pdata->gpio_cs;
333 err = spi_add_device(ucmb->sdev);
334 if (err) {
335 printk(KERN_ERR PFX "Failed to add SPI device\n");
336 goto err_free_spi_device;
337 }
338
339 /* Create the Misc char device. */
340
341 ucmb->mdev.minor = MISC_DYNAMIC_MINOR;
342 ucmb->mdev.name = pdata->name;
343 ucmb->mdev.parent = &pdev->dev;
344 ucmb->mdev_fops.read = ucmb_read;
345 ucmb->mdev_fops.write = ucmb_write;
346 ucmb->mdev.fops = &ucmb->mdev_fops;
347
348 err = misc_register(&ucmb->mdev);
349 if (err) {
350 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
351 ucmb->mdev.name);
352 goto err_unreg_spi_device;
353 }
354
355 platform_set_drvdata(pdev, ucmb);
356
357 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
358
359 return 0;
360
361 err_unreg_spi_device:
362 spi_unregister_device(ucmb->sdev);
363 err_free_spi_device:
364 spi_dev_put(ucmb->sdev);
365 err_unreg_spi_gpio_pdev:
366 platform_device_unregister(&ucmb->spi_gpio_pdev);
367 err_free_ucmb:
368 kfree(ucmb);
369
370 return err;
371 }
372
373 static int __devexit ucmb_remove(struct platform_device *pdev)
374 {
375 struct ucmb *ucmb = platform_get_drvdata(pdev);
376 int err;
377
378 err = misc_deregister(&ucmb->mdev);
379 if (err) {
380 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
381 ucmb->mdev.name);
382 }
383 spi_unregister_device(ucmb->sdev);
384 spi_dev_put(ucmb->sdev);
385 platform_device_unregister(&ucmb->spi_gpio_pdev);
386
387 kfree(ucmb);
388 platform_set_drvdata(pdev, NULL);
389
390 return 0;
391 }
392
393 static struct platform_driver ucmb_driver = {
394 .driver = {
395 .name = "ucmb",
396 .owner = THIS_MODULE,
397 },
398 .probe = ucmb_probe,
399 .remove = __devexit_p(ucmb_probe),
400 };
401
402 static int ucmb_modinit(void)
403 {
404 struct ucmb_platform_data *pdata;
405 struct platform_device *pdev;
406 int err, i;
407
408 printk(KERN_INFO "Microcontroller message bus driver\n");
409
410 err = platform_driver_register(&ucmb_driver);
411 if (err) {
412 printk(KERN_ERR PFX "Failed to register platform driver\n");
413 return err;
414 }
415 err = spi_register_driver(&ucmb_spi_driver);
416 if (err) {
417 printk(KERN_ERR PFX "Failed to register SPI driver\n");
418 platform_driver_unregister(&ucmb_driver);
419 return err;
420 }
421
422 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
423 pdata = &ucmb_list[i];
424
425 pdev = platform_device_alloc("ucmb", i);
426 if (!pdev) {
427 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
428 break;
429 }
430 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
431 if (err) {
432 printk(KERN_ERR PFX "Failed to add platform data.\n");
433 platform_device_put(pdev);
434 break;
435 }
436 err = platform_device_add(pdev);
437 if (err) {
438 printk(KERN_ERR PFX "Failed to register platform device.\n");
439 platform_device_put(pdev);
440 break;
441 }
442 pdata->pdev = pdev;
443 }
444
445 return 0;
446 }
447 module_init(ucmb_modinit);
448
449 static void ucmb_modexit(void)
450 {
451 struct ucmb_platform_data *pdata;
452 int i;
453
454 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
455 pdata = &ucmb_list[i];
456
457 if (pdata->pdev) {
458 platform_device_unregister(pdata->pdev);
459 platform_device_put(pdata->pdev);
460 }
461 }
462 spi_unregister_driver(&ucmb_spi_driver);
463 platform_driver_unregister(&ucmb_driver);
464 }
465 module_exit(ucmb_modexit);