spidev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. if (u_tmp->rx_buf) {
  143. k_tmp->rx_buf = buf;
  144. if (!access_ok(VERIFY_WRITE, u_tmp->rx_buf, u_tmp->len))
  145. goto done;
  146. }
  147. if (u_tmp->tx_buf) {
  148. k_tmp->tx_buf = buf;
  149. if (copy_from_user(buf, (const u8 __user *)u_tmp->tx_buf,
  150. u_tmp->len))
  151. goto done;
  152. }
  153. total += k_tmp->len;
  154. if (total > bufsiz) {
  155. status = -EMSGSIZE;
  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. retval = -EFAULT;
  320. break;
  321. }
  322. /* translate to spi_message, execute */
  323. retval = spidev_message(spidev, ioc, n_ioc);
  324. kfree(ioc);
  325. break;
  326. }
  327. return retval;
  328. }
  329. static int spidev_open(struct inode *inode, struct file *filp)
  330. {
  331. struct spidev_data *spidev;
  332. int status = -ENXIO;
  333. mutex_lock(&device_list_lock);
  334. list_for_each_entry(spidev, &device_list, device_entry) {
  335. if (spidev->dev.devt == inode->i_rdev) {
  336. status = 0;
  337. break;
  338. }
  339. }
  340. if (status == 0) {
  341. if (!spidev->buffer) {
  342. spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
  343. if (!spidev->buffer) {
  344. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  345. status = -ENOMEM;
  346. }
  347. }
  348. if (status == 0) {
  349. spidev->users++;
  350. filp->private_data = spidev;
  351. nonseekable_open(inode, filp);
  352. }
  353. } else
  354. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  355. mutex_unlock(&device_list_lock);
  356. return status;
  357. }
  358. static int spidev_release(struct inode *inode, struct file *filp)
  359. {
  360. struct spidev_data *spidev;
  361. int status = 0;
  362. mutex_lock(&device_list_lock);
  363. spidev = filp->private_data;
  364. filp->private_data = NULL;
  365. spidev->users--;
  366. if (!spidev->users) {
  367. kfree(spidev->buffer);
  368. spidev->buffer = NULL;
  369. }
  370. mutex_unlock(&device_list_lock);
  371. return status;
  372. }
  373. static struct file_operations spidev_fops = {
  374. .owner = THIS_MODULE,
  375. /* REVISIT switch to aio primitives, so that userspace
  376. * gets more complete API coverage. It'll simplify things
  377. * too, except for the locking.
  378. */
  379. .write = spidev_write,
  380. .read = spidev_read,
  381. .ioctl = spidev_ioctl,
  382. .open = spidev_open,
  383. .release = spidev_release,
  384. };
  385. /*-------------------------------------------------------------------------*/
  386. /* The main reason to have this class is to make mdev/udev create the
  387. * /dev/spidevB.C character device nodes exposing our userspace API.
  388. * It also simplifies memory management.
  389. */
  390. static void spidev_classdev_release(struct device *dev)
  391. {
  392. struct spidev_data *spidev;
  393. spidev = container_of(dev, struct spidev_data, dev);
  394. kfree(spidev);
  395. }
  396. static struct class spidev_class = {
  397. .name = "spidev",
  398. .owner = THIS_MODULE,
  399. .dev_release = spidev_classdev_release,
  400. };
  401. /*-------------------------------------------------------------------------*/
  402. static int spidev_probe(struct spi_device *spi)
  403. {
  404. struct spidev_data *spidev;
  405. int status;
  406. unsigned long minor;
  407. /* Allocate driver data */
  408. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  409. if (!spidev)
  410. return -ENOMEM;
  411. /* Initialize the driver data */
  412. spidev->spi = spi;
  413. mutex_init(&spidev->buf_lock);
  414. INIT_LIST_HEAD(&spidev->device_entry);
  415. /* If we can allocate a minor number, hook up this device.
  416. * Reusing minors is fine so long as udev or mdev is working.
  417. */
  418. mutex_lock(&device_list_lock);
  419. minor = find_first_zero_bit(minors, ARRAY_SIZE(minors));
  420. if (minor < N_SPI_MINORS) {
  421. spidev->dev.parent = &spi->dev;
  422. spidev->dev.class = &spidev_class;
  423. spidev->dev.devt = MKDEV(SPIDEV_MAJOR, minor);
  424. snprintf(spidev->dev.bus_id, sizeof spidev->dev.bus_id,
  425. "spidev%d.%d",
  426. spi->master->bus_num, spi->chip_select);
  427. status = device_register(&spidev->dev);
  428. } else {
  429. dev_dbg(&spi->dev, "no minor number available!\n");
  430. status = -ENODEV;
  431. }
  432. if (status == 0) {
  433. set_bit(minor, minors);
  434. dev_set_drvdata(&spi->dev, spidev);
  435. list_add(&spidev->device_entry, &device_list);
  436. }
  437. mutex_unlock(&device_list_lock);
  438. if (status != 0)
  439. kfree(spidev);
  440. return status;
  441. }
  442. static int spidev_remove(struct spi_device *spi)
  443. {
  444. struct spidev_data *spidev = dev_get_drvdata(&spi->dev);
  445. mutex_lock(&device_list_lock);
  446. list_del(&spidev->device_entry);
  447. dev_set_drvdata(&spi->dev, NULL);
  448. clear_bit(MINOR(spidev->dev.devt), minors);
  449. device_unregister(&spidev->dev);
  450. mutex_unlock(&device_list_lock);
  451. return 0;
  452. }
  453. static struct spi_driver spidev_spi = {
  454. .driver = {
  455. .name = "spidev",
  456. .owner = THIS_MODULE,
  457. },
  458. .probe = spidev_probe,
  459. .remove = __devexit_p(spidev_remove),
  460. /* NOTE: suspend/resume methods are not necessary here.
  461. * We don't do anything except pass the requests to/from
  462. * the underlying controller. The refrigerator handles
  463. * most issues; the controller driver handles the rest.
  464. */
  465. };
  466. /*-------------------------------------------------------------------------*/
  467. static int __init spidev_init(void)
  468. {
  469. int status;
  470. /* Claim our 256 reserved device numbers. Then register a class
  471. * that will key udev/mdev to add/remove /dev nodes. Last, register
  472. * the driver which manages those device numbers.
  473. */
  474. BUILD_BUG_ON(N_SPI_MINORS > 256);
  475. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  476. if (status < 0)
  477. return status;
  478. status = class_register(&spidev_class);
  479. if (status < 0) {
  480. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  481. return status;
  482. }
  483. status = spi_register_driver(&spidev_spi);
  484. if (status < 0) {
  485. class_unregister(&spidev_class);
  486. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  487. }
  488. return status;
  489. }
  490. module_init(spidev_init);
  491. static void __exit spidev_exit(void)
  492. {
  493. spi_unregister_driver(&spidev_spi);
  494. class_unregister(&spidev_class);
  495. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  496. }
  497. module_exit(spidev_exit);
  498. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  499. MODULE_DESCRIPTION("User mode SPI device interface");
  500. MODULE_LICENSE("GPL");