spidev.c 15 KB

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