ucmb: Fix the lowlevel protocol
[openwrt/svn-archive/archive.git] / package / 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 #define 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 #define UCMB_MAX_MSG_LEN 0x200
64
65 enum ucmb_status_code {
66 UCMB_STAT_OK = 0,
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 */
71 };
72
73
74 static int ucmb_spi_busnum_count = 1337;
75
76
77 static struct ucmb_platform_data ucmb_list[] = {
78 { //FIXME don't define it here.
79 .name = "ucmb",
80 .gpio_cs = 3,
81 .gpio_sck = 0,
82 .gpio_miso = 1,
83 .gpio_mosi = 2,
84 .mode = SPI_MODE_0,
85 .max_speed_hz = 128000, /* Hz */
86 .msg_delay_ms = 1, /* mS */
87 },
88 };
89
90
91 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
92 {
93 return 0;
94 }
95
96 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
97 {
98 return 0;
99 }
100
101 static struct spi_driver ucmb_spi_driver = {
102 .driver = {
103 .name = "ucmb",
104 .bus = &spi_bus_type,
105 .owner = THIS_MODULE,
106 },
107 .probe = ucmb_spi_probe,
108 .remove = __devexit_p(ucmb_spi_remove),
109 };
110
111 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
112 {
113 switch (code) {
114 case UCMB_STAT_OK:
115 return 0;
116 case UCMB_STAT_EPROTO:
117 return -EPROTO;
118 case UCMB_STAT_ENOMEM:
119 return -ENOMEM;
120 case UCMB_STAT_E2BIG:
121 return -E2BIG;
122 case UCMB_STAT_ECRC:
123 return -EBADMSG;
124 }
125 return -EBUSY;
126 }
127
128 static inline struct ucmb * filp_to_ucmb(struct file *filp)
129 {
130 return container_of(filp->f_op, struct ucmb, mdev_fops);
131 }
132
133 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
134 size_t size, loff_t *offp)
135 {
136 struct ucmb *ucmb = filp_to_ucmb(filp);
137 u8 *buf;
138 int res, err;
139 struct ucmb_message_hdr hdr;
140 struct ucmb_message_footer footer;
141 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
142 u16 crc = 0xFFFF;
143
144 size = min_t(size_t, size, PAGE_SIZE);
145
146 err = -ENOMEM;
147 buf = (char *)__get_free_page(GFP_KERNEL);
148 if (!buf)
149 goto out;
150
151 err = spi_read(ucmb->sdev, (u8 *)&hdr, sizeof(hdr));
152 if (err)
153 goto out_free;
154 #ifdef DEBUG
155 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
156 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
157 #endif
158 err = -EPROTO;
159 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
160 goto out_free;
161 err = -ENOBUFS;
162 if (size < le16_to_cpu(hdr.len))
163 goto out_free;
164 size = le16_to_cpu(hdr.len);
165 err = spi_read(ucmb->sdev, buf, size);
166 if (err)
167 goto out_free;
168 err = spi_read(ucmb->sdev, (u8 *)&footer, sizeof(footer));
169 if (err)
170 goto out_free;
171
172 crc = crc16(crc, &hdr, sizeof(hdr));
173 crc = crc16(crc, buf, size);
174 crc ^= 0xFFFF;
175 if (crc != le16_to_cpu(footer.crc)) {
176 err = -EPROTO;
177 status.code = UCMB_STAT_ECRC;
178 goto out_send_status;
179 }
180
181 if (copy_to_user(user_buf, buf, size)) {
182 err = -EFAULT;
183 status.code = UCMB_STAT_ENOMEM;
184 goto out_send_status;
185 }
186
187 status.code = UCMB_STAT_OK;
188 err = 0;
189
190 out_send_status:
191 res = spi_write(ucmb->sdev, (u8 *)&status, sizeof(status));
192 if (res && !err)
193 err = res;
194 out_free:
195 free_page((unsigned long)buf);
196 out:
197 return err ? err : size;
198 }
199
200 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
201 size_t size, loff_t *offp)
202 {
203 struct ucmb *ucmb = filp_to_ucmb(filp);
204 u8 *buf;
205 int err;
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;
213
214 err = -ENOMEM;
215 buf = (char *)__get_free_page(GFP_KERNEL);
216 if (!buf)
217 goto out;
218
219 size = min_t(size_t, PAGE_SIZE, size);
220 size = min_t(size_t, UCMB_MAX_MSG_LEN, size);
221 err = -EFAULT;
222 if (copy_from_user(buf, user_buf, size))
223 goto out_free;
224 hdr.len = cpu_to_le16(size);
225
226 footer.crc = crc16(footer.crc, &hdr, sizeof(hdr));
227 footer.crc = crc16(footer.crc, buf, size);
228 footer.crc ^= 0xFFFF;
229
230 spi_message_init(&spi_msg);
231
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);
236
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);
241
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);
246
247 /* Send the message, including header. */
248 err = spi_sync(ucmb->sdev, &spi_msg);
249 if (err)
250 goto out_free;
251
252 /* The microcontroller deserves some time to process the message. */
253 if (ucmb->msg_delay_ms)
254 msleep(ucmb->msg_delay_ms);
255
256 /* Get the status code. */
257 err = spi_read(ucmb->sdev, (u8 *)&status, sizeof(status));
258 if (err)
259 goto out_free;
260 #ifdef DEBUG
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));
263 #endif
264 err = -EPROTO;
265 if (status.magic != cpu_to_le16(UCMB_MAGIC))
266 goto out_free;
267 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
268 if (err)
269 goto out_free;
270
271 out_free:
272 free_page((unsigned long)buf);
273 out:
274 return err ? err : size;
275 }
276
277 static int __devinit ucmb_probe(struct platform_device *pdev)
278 {
279 struct ucmb_platform_data *pdata;
280 struct ucmb *ucmb;
281 int err;
282 const int bus_num = ucmb_spi_busnum_count++;
283 struct spi_bitbang *bb;
284
285 pdata = pdev->dev.platform_data;
286 if (!pdata)
287 return -ENXIO;
288
289 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
290 if (!ucmb)
291 return -ENOMEM;
292 ucmb->msg_delay_ms = pdata->msg_delay_ms;
293
294 /* Create the SPI GPIO bus master. */
295
296 #ifdef CONFIG_SPI_GPIO_MODULE
297 err = request_module("spi_gpio");
298 if (err)
299 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
300 #endif /* CONFIG_SPI_GPIO_MODULE */
301
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;
306
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;
310
311 err = platform_device_register(&ucmb->spi_gpio_pdev);
312 if (err) {
313 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
314 goto err_free_ucmb;
315 }
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;
320 }
321
322 /* Create the SPI device. */
323
324 ucmb->sdev = spi_alloc_device(bb->master);
325 if (!ucmb->sdev) {
326 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
327 goto err_unreg_spi_gpio_pdev;
328 }
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);
336 if (err) {
337 printk(KERN_ERR PFX "Failed to add SPI device\n");
338 goto err_free_spi_device;
339 }
340
341 /* Create the Misc char device. */
342
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;
349
350 err = misc_register(&ucmb->mdev);
351 if (err) {
352 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
353 ucmb->mdev.name);
354 goto err_unreg_spi_device;
355 }
356
357 platform_set_drvdata(pdev, ucmb);
358
359 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
360
361 return 0;
362
363 err_unreg_spi_device:
364 spi_unregister_device(ucmb->sdev);
365 err_free_spi_device:
366 spi_dev_put(ucmb->sdev);
367 err_unreg_spi_gpio_pdev:
368 platform_device_unregister(&ucmb->spi_gpio_pdev);
369 err_free_ucmb:
370 kfree(ucmb);
371
372 return err;
373 }
374
375 static int __devexit ucmb_remove(struct platform_device *pdev)
376 {
377 struct ucmb *ucmb = platform_get_drvdata(pdev);
378 int err;
379
380 err = misc_deregister(&ucmb->mdev);
381 if (err) {
382 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
383 ucmb->mdev.name);
384 }
385 spi_unregister_device(ucmb->sdev);
386 spi_dev_put(ucmb->sdev);
387 platform_device_unregister(&ucmb->spi_gpio_pdev);
388
389 kfree(ucmb);
390 platform_set_drvdata(pdev, NULL);
391
392 return 0;
393 }
394
395 static struct platform_driver ucmb_driver = {
396 .driver = {
397 .name = "ucmb",
398 .owner = THIS_MODULE,
399 },
400 .probe = ucmb_probe,
401 .remove = __devexit_p(ucmb_probe),
402 };
403
404 static int ucmb_modinit(void)
405 {
406 struct ucmb_platform_data *pdata;
407 struct platform_device *pdev;
408 int err, i;
409
410 printk(KERN_INFO "Microcontroller message bus driver\n");
411
412 err = platform_driver_register(&ucmb_driver);
413 if (err) {
414 printk(KERN_ERR PFX "Failed to register platform driver\n");
415 return err;
416 }
417 err = spi_register_driver(&ucmb_spi_driver);
418 if (err) {
419 printk(KERN_ERR PFX "Failed to register SPI driver\n");
420 platform_driver_unregister(&ucmb_driver);
421 return err;
422 }
423
424 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
425 pdata = &ucmb_list[i];
426
427 pdev = platform_device_alloc("ucmb", i);
428 if (!pdev) {
429 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
430 break;
431 }
432 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
433 if (err) {
434 printk(KERN_ERR PFX "Failed to add platform data.\n");
435 platform_device_put(pdev);
436 break;
437 }
438 err = platform_device_add(pdev);
439 if (err) {
440 printk(KERN_ERR PFX "Failed to register platform device.\n");
441 platform_device_put(pdev);
442 break;
443 }
444 pdata->pdev = pdev;
445 }
446
447 return 0;
448 }
449 module_init(ucmb_modinit);
450
451 static void ucmb_modexit(void)
452 {
453 struct ucmb_platform_data *pdata;
454 int i;
455
456 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
457 pdata = &ucmb_list[i];
458
459 if (pdata->pdev) {
460 platform_device_unregister(pdata->pdev);
461 platform_device_put(pdata->pdev);
462 }
463 }
464 spi_unregister_driver(&ucmb_spi_driver);
465 platform_driver_unregister(&ucmb_driver);
466 }
467 module_exit(ucmb_modexit);