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