spidev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * spidev.c -- simple synchronous userspace interface to SPI devices
  3. *
  4. * Copyright (C) 2006 SWAPP
  5. * Andrea Paterniani <a.paterniani@swapp-eng.it>
  6. * Copyright (C) 2007 David Brownell (simplification, cleanup)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/fs.h>
  26. #include <linux/device.h>
  27. #include <linux/list.h>
  28. #include <linux/errno.h>
  29. #include <linux/mutex.h>
  30. #include <linux/slab.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/spi/spidev.h>
  33. #include <asm/uaccess.h>
  34. /*
  35. * This supports acccess to SPI devices using normal userspace I/O calls.
  36. * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
  37. * and often mask message boundaries, full SPI support requires full duplex
  38. * transfers. There are several kinds of of internal message boundaries to
  39. * handle chipselect management and other protocol options.
  40. *
  41. * SPI has a character major number assigned. We allocate minor numbers
  42. * dynamically using a bitmask. You must use hotplug tools, such as udev
  43. * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
  44. * nodes, since there is no fixed association of minor numbers with any
  45. * particular SPI bus or device.
  46. */
  47. #define SPIDEV_MAJOR 153 /* assigned */
  48. #define N_SPI_MINORS 32 /* ... up to 256 */
  49. static unsigned long minors[N_SPI_MINORS / BITS_PER_LONG];
  50. /* Bit masks for spi_device.mode management */
  51. #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL)
  52. struct spidev_data {
  53. struct device dev;
  54. struct spi_device *spi;
  55. struct list_head device_entry;
  56. struct mutex buf_lock;
  57. unsigned users;
  58. u8 *buffer;
  59. };
  60. static LIST_HEAD(device_list);
  61. static DEFINE_MUTEX(device_list_lock);
  62. static unsigned bufsiz = 4096;
  63. module_param(bufsiz, uint, S_IRUGO);
  64. MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
  65. /*-------------------------------------------------------------------------*/
  66. /* Read-only message with current device setup */
  67. static ssize_t
  68. spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  69. {
  70. struct spidev_data *spidev;
  71. struct spi_device *spi;
  72. ssize_t status = 0;
  73. /* chipselect only toggles at start or end of operation */
  74. if (count > bufsiz)
  75. return -EMSGSIZE;
  76. spidev = filp->private_data;
  77. spi = spidev->spi;
  78. mutex_lock(&spidev->buf_lock);
  79. status = spi_read(spi, spidev->buffer, count);
  80. if (status == 0) {
  81. unsigned long missing;
  82. missing = copy_to_user(buf, spidev->buffer, count);
  83. if (count && missing == count)
  84. status = -EFAULT;
  85. else
  86. status = count - missing;
  87. }
  88. mutex_unlock(&spidev->buf_lock);
  89. return status;
  90. }
  91. /* Write-only message with current device setup */
  92. static ssize_t
  93. spidev_write(struct file *filp, const char __user *buf,
  94. size_t count, loff_t *f_pos)
  95. {
  96. struct spidev_data *spidev;
  97. struct spi_device *spi;
  98. ssize_t status = 0;
  99. unsigned long missing;
  100. /* chipselect only toggles at start or end of operation */
  101. if (count > bufsiz)
  102. return -EMSGSIZE;
  103. spidev = filp->private_data;
  104. spi = spidev->spi;
  105. mutex_lock(&spidev->buf_lock);
  106. missing = copy_from_user(spidev->buffer, buf, count);
  107. if (missing == 0) {
  108. status = spi_write(spi, spidev->buffer, count);
  109. if (status == 0)
  110. status = count;
  111. } else
  112. status = -EFAULT;
  113. mutex_unlock(&spidev->buf_lock);
  114. return status;
  115. }
  116. static int spidev_message(struct spidev_data *spidev,
  117. struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
  118. {
  119. struct spi_message msg;
  120. struct spi_transfer *k_xfers;
  121. struct spi_transfer *k_tmp;
  122. struct spi_ioc_transfer *u_tmp;
  123. struct spi_device *spi = spidev->spi;
  124. unsigned n, total;
  125. u8 *buf;
  126. int status = -EFAULT;
  127. spi_message_init(&msg);
  128. k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
  129. if (k_xfers == NULL)
  130. return -ENOMEM;
  131. /* Construct spi_message, copying any tx data to bounce buffer.
  132. * We walk the array of user-provided transfers, using each one
  133. * to initialize a kernel version of the same transfer.
  134. */
  135. mutex_lock(&spidev->buf_lock);
  136. buf = spidev->buffer;
  137. total = 0;
  138. for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  139. n;
  140. n--, k_tmp++, u_tmp++) {
  141. k_tmp->len = u_tmp->len;
  142. total += k_tmp->len;
  143. if (total > bufsiz) {
  144. status = -EMSGSIZE;
  145. goto done;
  146. }
  147. if (u_tmp->rx_buf) {
  148. k_tmp->rx_buf = buf;
  149. if (!access_ok(VERIFY_WRITE, u_tmp->rx_buf, u_tmp->len))
  150. goto done;
  151. }
  152. if (u_tmp->tx_buf) {
  153. k_tmp->tx_buf = buf;
  154. if (copy_from_user(buf, (const u8 __user *)u_tmp->tx_buf,
  155. u_tmp->len))
  156. goto done;
  157. }
  158. buf += k_tmp->len;
  159. k_tmp->cs_change = !!u_tmp->cs_change;
  160. k_tmp->bits_per_word = u_tmp->bits_per_word;
  161. k_tmp->delay_usecs = u_tmp->delay_usecs;
  162. k_tmp->speed_hz = u_tmp->speed_hz;
  163. #ifdef VERBOSE
  164. dev_dbg(&spi->dev,
  165. " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
  166. u_tmp->len,
  167. u_tmp->rx_buf ? "rx " : "",
  168. u_tmp->tx_buf ? "tx " : "",
  169. u_tmp->cs_change ? "cs " : "",
  170. u_tmp->bits_per_word ? : spi->bits_per_word,
  171. u_tmp->delay_usecs,
  172. u_tmp->speed_hz ? : spi->max_speed_hz);
  173. #endif
  174. spi_message_add_tail(k_tmp, &msg);
  175. }
  176. status = spi_sync(spi, &msg);
  177. if (status < 0)
  178. goto done;
  179. /* copy any rx data out of bounce buffer */
  180. buf = spidev->buffer;
  181. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  182. if (u_tmp->rx_buf) {
  183. if (__copy_to_user((u8 __user *)u_tmp->rx_buf, buf,
  184. u_tmp->len)) {
  185. status = -EFAULT;
  186. goto done;
  187. }
  188. }
  189. buf += u_tmp->len;
  190. }
  191. status = total;
  192. done:
  193. mutex_unlock(&spidev->buf_lock);
  194. kfree(k_xfers);
  195. return status;
  196. }
  197. static int
  198. spidev_ioctl(struct inode *inode, struct file *filp,
  199. unsigned int cmd, unsigned long arg)
  200. {
  201. int err = 0;
  202. int retval = 0;
  203. struct spidev_data *spidev;
  204. struct spi_device *spi;
  205. u32 tmp;
  206. unsigned n_ioc;
  207. struct spi_ioc_transfer *ioc;
  208. /* Check type and command number */
  209. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  210. return -ENOTTY;
  211. /* Check access direction once here; don't repeat below.
  212. * IOC_DIR is from the user perspective, while access_ok is
  213. * from the kernel perspective; so they look reversed.
  214. */
  215. if (_IOC_DIR(cmd) & _IOC_READ)
  216. err = !access_ok(VERIFY_WRITE,
  217. (void __user *)arg, _IOC_SIZE(cmd));
  218. if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  219. err = !access_ok(VERIFY_READ,
  220. (void __user *)arg, _IOC_SIZE(cmd));
  221. if (err)
  222. return -EFAULT;
  223. spidev = filp->private_data;
  224. spi = spidev->spi;
  225. switch (cmd) {
  226. /* read requests */
  227. case SPI_IOC_RD_MODE:
  228. retval = __put_user(spi->mode & SPI_MODE_MASK,
  229. (__u8 __user *)arg);
  230. break;
  231. case SPI_IOC_RD_LSB_FIRST:
  232. retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  233. (__u8 __user *)arg);
  234. break;
  235. case SPI_IOC_RD_BITS_PER_WORD:
  236. retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  237. break;
  238. case SPI_IOC_RD_MAX_SPEED_HZ:
  239. retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
  240. break;
  241. /* write requests */
  242. case SPI_IOC_WR_MODE:
  243. retval = __get_user(tmp, (u8 __user *)arg);
  244. if (retval == 0) {
  245. u8 save = spi->mode;
  246. if (tmp & ~SPI_MODE_MASK) {
  247. retval = -EINVAL;
  248. break;
  249. }
  250. tmp |= spi->mode & ~SPI_MODE_MASK;
  251. spi->mode = (u8)tmp;
  252. retval = spi_setup(spi);
  253. if (retval < 0)
  254. spi->mode = save;
  255. else
  256. dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
  257. }
  258. break;
  259. case SPI_IOC_WR_LSB_FIRST:
  260. retval = __get_user(tmp, (__u8 __user *)arg);
  261. if (retval == 0) {
  262. u8 save = spi->mode;
  263. if (tmp)
  264. spi->mode |= SPI_LSB_FIRST;
  265. else
  266. spi->mode &= ~SPI_LSB_FIRST;
  267. retval = spi_setup(spi);
  268. if (retval < 0)
  269. spi->mode = save;
  270. else
  271. dev_dbg(&spi->dev, "%csb first\n",
  272. tmp ? 'l' : 'm');
  273. }
  274. break;
  275. case SPI_IOC_WR_BITS_PER_WORD:
  276. retval = __get_user(tmp, (__u8 __user *)arg);
  277. if (retval == 0) {
  278. u8 save = spi->bits_per_word;
  279. spi->bits_per_word = tmp;
  280. retval = spi_setup(spi);
  281. if (retval < 0)
  282. spi->bits_per_word = save;
  283. else
  284. dev_dbg(&spi->dev, "%d bits per word\n", tmp);
  285. }
  286. break;
  287. case SPI_IOC_WR_MAX_SPEED_HZ:
  288. retval = __get_user(tmp, (__u32 __user *)arg);
  289. if (retval == 0) {
  290. u32 save = spi->max_speed_hz;
  291. spi->max_speed_hz = tmp;
  292. retval = spi_setup(spi);
  293. if (retval < 0)
  294. spi->max_speed_hz = save;
  295. else
  296. dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
  297. }
  298. break;
  299. default:
  300. /* segmented and/or full-duplex I/O request */
  301. if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  302. || _IOC_DIR(cmd) != _IOC_WRITE)
  303. return -ENOTTY;
  304. tmp = _IOC_SIZE(cmd);
  305. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
  306. retval = -EINVAL;
  307. break;
  308. }
  309. n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  310. if (n_ioc == 0)
  311. break;
  312. /* copy into scratch area */
  313. ioc = kmalloc(tmp, GFP_KERNEL);
  314. if (!ioc) {
  315. retval = -ENOMEM;
  316. break;
  317. }
  318. if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
  319. kfree(ioc);
  320. retval = -EFAULT;
  321. break;
  322. }
  323. /* translate to spi_message, execute */
  324. retval = spidev_message(spidev, ioc, n_ioc);
  325. kfree(ioc);
  326. break;
  327. }
  328. return retval;
  329. }
  330. static int spidev_open(struct inode *inode, struct file *filp)
  331. {
  332. struct spidev_data *spidev;
  333. int status = -ENXIO;
  334. mutex_lock(&device_list_lock);
  335. list_for_each_entry(spidev, &device_list, device_entry) {
  336. if (spidev->dev.devt == inode->i_rdev) {
  337. status = 0;
  338. break;
  339. }
  340. }
  341. if (status == 0) {
  342. if (!spidev->buffer) {
  343. spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
  344. if (!spidev->buffer) {
  345. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  346. status = -ENOMEM;
  347. }
  348. }
  349. if (status == 0) {
  350. spidev->users++;
  351. filp->private_data = spidev;
  352. nonseekable_open(inode, filp);
  353. }
  354. } else
  355. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  356. mutex_unlock(&device_list_lock);
  357. return status;
  358. }
  359. static int spidev_release(struct inode *inode, struct file *filp)
  360. {
  361. struct spidev_data *spidev;
  362. int status = 0;
  363. mutex_lock(&device_list_lock);
  364. spidev = filp->private_data;
  365. filp->private_data = NULL;
  366. spidev->users--;
  367. if (!spidev->users) {
  368. kfree(spidev->buffer);
  369. spidev->buffer = NULL;
  370. }
  371. mutex_unlock(&device_list_lock);
  372. return status;
  373. }
  374. static struct file_operations spidev_fops = {
  375. .owner = THIS_MODULE,
  376. /* REVISIT switch to aio primitives, so that userspace
  377. * gets more complete API coverage. It'll simplify things
  378. * too, except for the locking.
  379. */
  380. .write = spidev_write,
  381. .read = spidev_read,
  382. .ioctl = spidev_ioctl,
  383. .open = spidev_open,
  384. .release = spidev_release,
  385. };
  386. /*-------------------------------------------------------------------------*/
  387. /* The main reason to have this class is to make mdev/udev create the
  388. * /dev/spidevB.C character device nodes exposing our userspace API.
  389. * It also simplifies memory management.
  390. */
  391. static void spidev_classdev_release(struct device *dev)
  392. {
  393. struct spidev_data *spidev;
  394. spidev = container_of(dev, struct spidev_data, dev);
  395. kfree(spidev);
  396. }
  397. static struct class spidev_class = {
  398. .name = "spidev",
  399. .owner = THIS_MODULE,
  400. .dev_release = spidev_classdev_release,
  401. };
  402. /*-------------------------------------------------------------------------*/
  403. static int spidev_probe(struct spi_device *spi)
  404. {
  405. struct spidev_data *spidev;
  406. int status;
  407. unsigned long minor;
  408. /* Allocate driver data */
  409. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  410. if (!spidev)
  411. return -ENOMEM;
  412. /* Initialize the driver data */
  413. spidev->spi = spi;
  414. mutex_init(&spidev->buf_lock);
  415. INIT_LIST_HEAD(&spidev->device_entry);
  416. /* If we can allocate a minor number, hook up this device.
  417. * Reusing minors is fine so long as udev or mdev is working.
  418. */
  419. mutex_lock(&device_list_lock);
  420. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  421. if (minor < N_SPI_MINORS) {
  422. spidev->dev.parent = &spi->dev;
  423. spidev->dev.class = &spidev_class;
  424. spidev->dev.devt = MKDEV(SPIDEV_MAJOR, minor);
  425. snprintf(spidev->dev.bus_id, sizeof spidev->dev.bus_id,
  426. "spidev%d.%d",
  427. spi->master->bus_num, spi->chip_select);
  428. status = device_register(&spidev->dev);
  429. } else {
  430. dev_dbg(&spi->dev, "no minor number available!\n");
  431. status = -ENODEV;
  432. }
  433. if (status == 0) {
  434. set_bit(minor, minors);
  435. dev_set_drvdata(&spi->dev, spidev);
  436. list_add(&spidev->device_entry, &device_list);
  437. }
  438. mutex_unlock(&device_list_lock);
  439. if (status != 0)
  440. kfree(spidev);
  441. return status;
  442. }
  443. static int spidev_remove(struct spi_device *spi)
  444. {
  445. struct spidev_data *spidev = dev_get_drvdata(&spi->dev);
  446. mutex_lock(&device_list_lock);
  447. list_del(&spidev->device_entry);
  448. dev_set_drvdata(&spi->dev, NULL);
  449. clear_bit(MINOR(spidev->dev.devt), minors);
  450. device_unregister(&spidev->dev);
  451. mutex_unlock(&device_list_lock);
  452. return 0;
  453. }
  454. static struct spi_driver spidev_spi = {
  455. .driver = {
  456. .name = "spidev",
  457. .owner = THIS_MODULE,
  458. },
  459. .probe = spidev_probe,
  460. .remove = __devexit_p(spidev_remove),
  461. /* NOTE: suspend/resume methods are not necessary here.
  462. * We don't do anything except pass the requests to/from
  463. * the underlying controller. The refrigerator handles
  464. * most issues; the controller driver handles the rest.
  465. */
  466. };
  467. /*-------------------------------------------------------------------------*/
  468. static int __init spidev_init(void)
  469. {
  470. int status;
  471. /* Claim our 256 reserved device numbers. Then register a class
  472. * that will key udev/mdev to add/remove /dev nodes. Last, register
  473. * the driver which manages those device numbers.
  474. */
  475. BUILD_BUG_ON(N_SPI_MINORS > 256);
  476. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  477. if (status < 0)
  478. return status;
  479. status = class_register(&spidev_class);
  480. if (status < 0) {
  481. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  482. return status;
  483. }
  484. status = spi_register_driver(&spidev_spi);
  485. if (status < 0) {
  486. class_unregister(&spidev_class);
  487. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  488. }
  489. return status;
  490. }
  491. module_init(spidev_init);
  492. static void __exit spidev_exit(void)
  493. {
  494. spi_unregister_driver(&spidev_spi);
  495. class_unregister(&spidev_class);
  496. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  497. }
  498. module_exit(spidev_exit);
  499. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  500. MODULE_DESCRIPTION("User mode SPI device interface");
  501. MODULE_LICENSE("GPL");