spidev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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, u_tmp->rx_buf, u_tmp->len))
  158. goto done;
  159. }
  160. if (u_tmp->tx_buf) {
  161. k_tmp->tx_buf = buf;
  162. if (copy_from_user(buf, (const u8 __user *)
  163. (ptrdiff_t) u_tmp->tx_buf,
  164. u_tmp->len))
  165. goto done;
  166. }
  167. buf += k_tmp->len;
  168. k_tmp->cs_change = !!u_tmp->cs_change;
  169. k_tmp->bits_per_word = u_tmp->bits_per_word;
  170. k_tmp->delay_usecs = u_tmp->delay_usecs;
  171. k_tmp->speed_hz = u_tmp->speed_hz;
  172. #ifdef VERBOSE
  173. dev_dbg(&spi->dev,
  174. " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
  175. u_tmp->len,
  176. u_tmp->rx_buf ? "rx " : "",
  177. u_tmp->tx_buf ? "tx " : "",
  178. u_tmp->cs_change ? "cs " : "",
  179. u_tmp->bits_per_word ? : spi->bits_per_word,
  180. u_tmp->delay_usecs,
  181. u_tmp->speed_hz ? : spi->max_speed_hz);
  182. #endif
  183. spi_message_add_tail(k_tmp, &msg);
  184. }
  185. status = spi_sync(spi, &msg);
  186. if (status < 0)
  187. goto done;
  188. /* copy any rx data out of bounce buffer */
  189. buf = spidev->buffer;
  190. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  191. if (u_tmp->rx_buf) {
  192. if (__copy_to_user((u8 __user *)
  193. (ptrdiff_t) u_tmp->rx_buf, buf,
  194. u_tmp->len)) {
  195. status = -EFAULT;
  196. goto done;
  197. }
  198. }
  199. buf += u_tmp->len;
  200. }
  201. status = total;
  202. done:
  203. mutex_unlock(&spidev->buf_lock);
  204. kfree(k_xfers);
  205. return status;
  206. }
  207. static int
  208. spidev_ioctl(struct inode *inode, struct file *filp,
  209. unsigned int cmd, unsigned long arg)
  210. {
  211. int err = 0;
  212. int retval = 0;
  213. struct spidev_data *spidev;
  214. struct spi_device *spi;
  215. u32 tmp;
  216. unsigned n_ioc;
  217. struct spi_ioc_transfer *ioc;
  218. /* Check type and command number */
  219. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  220. return -ENOTTY;
  221. /* Check access direction once here; don't repeat below.
  222. * IOC_DIR is from the user perspective, while access_ok is
  223. * from the kernel perspective; so they look reversed.
  224. */
  225. if (_IOC_DIR(cmd) & _IOC_READ)
  226. err = !access_ok(VERIFY_WRITE,
  227. (void __user *)arg, _IOC_SIZE(cmd));
  228. if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  229. err = !access_ok(VERIFY_READ,
  230. (void __user *)arg, _IOC_SIZE(cmd));
  231. if (err)
  232. return -EFAULT;
  233. spidev = filp->private_data;
  234. spi = spidev->spi;
  235. switch (cmd) {
  236. /* read requests */
  237. case SPI_IOC_RD_MODE:
  238. retval = __put_user(spi->mode & SPI_MODE_MASK,
  239. (__u8 __user *)arg);
  240. break;
  241. case SPI_IOC_RD_LSB_FIRST:
  242. retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  243. (__u8 __user *)arg);
  244. break;
  245. case SPI_IOC_RD_BITS_PER_WORD:
  246. retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  247. break;
  248. case SPI_IOC_RD_MAX_SPEED_HZ:
  249. retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
  250. break;
  251. /* write requests */
  252. case SPI_IOC_WR_MODE:
  253. retval = __get_user(tmp, (u8 __user *)arg);
  254. if (retval == 0) {
  255. u8 save = spi->mode;
  256. if (tmp & ~SPI_MODE_MASK) {
  257. retval = -EINVAL;
  258. break;
  259. }
  260. tmp |= spi->mode & ~SPI_MODE_MASK;
  261. spi->mode = (u8)tmp;
  262. retval = spi_setup(spi);
  263. if (retval < 0)
  264. spi->mode = save;
  265. else
  266. dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
  267. }
  268. break;
  269. case SPI_IOC_WR_LSB_FIRST:
  270. retval = __get_user(tmp, (__u8 __user *)arg);
  271. if (retval == 0) {
  272. u8 save = spi->mode;
  273. if (tmp)
  274. spi->mode |= SPI_LSB_FIRST;
  275. else
  276. spi->mode &= ~SPI_LSB_FIRST;
  277. retval = spi_setup(spi);
  278. if (retval < 0)
  279. spi->mode = save;
  280. else
  281. dev_dbg(&spi->dev, "%csb first\n",
  282. tmp ? 'l' : 'm');
  283. }
  284. break;
  285. case SPI_IOC_WR_BITS_PER_WORD:
  286. retval = __get_user(tmp, (__u8 __user *)arg);
  287. if (retval == 0) {
  288. u8 save = spi->bits_per_word;
  289. spi->bits_per_word = tmp;
  290. retval = spi_setup(spi);
  291. if (retval < 0)
  292. spi->bits_per_word = save;
  293. else
  294. dev_dbg(&spi->dev, "%d bits per word\n", tmp);
  295. }
  296. break;
  297. case SPI_IOC_WR_MAX_SPEED_HZ:
  298. retval = __get_user(tmp, (__u32 __user *)arg);
  299. if (retval == 0) {
  300. u32 save = spi->max_speed_hz;
  301. spi->max_speed_hz = tmp;
  302. retval = spi_setup(spi);
  303. if (retval < 0)
  304. spi->max_speed_hz = save;
  305. else
  306. dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
  307. }
  308. break;
  309. default:
  310. /* segmented and/or full-duplex I/O request */
  311. if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  312. || _IOC_DIR(cmd) != _IOC_WRITE)
  313. return -ENOTTY;
  314. tmp = _IOC_SIZE(cmd);
  315. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
  316. retval = -EINVAL;
  317. break;
  318. }
  319. n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  320. if (n_ioc == 0)
  321. break;
  322. /* copy into scratch area */
  323. ioc = kmalloc(tmp, GFP_KERNEL);
  324. if (!ioc) {
  325. retval = -ENOMEM;
  326. break;
  327. }
  328. if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
  329. kfree(ioc);
  330. retval = -EFAULT;
  331. break;
  332. }
  333. /* translate to spi_message, execute */
  334. retval = spidev_message(spidev, ioc, n_ioc);
  335. kfree(ioc);
  336. break;
  337. }
  338. return retval;
  339. }
  340. static int spidev_open(struct inode *inode, struct file *filp)
  341. {
  342. struct spidev_data *spidev;
  343. int status = -ENXIO;
  344. mutex_lock(&device_list_lock);
  345. list_for_each_entry(spidev, &device_list, device_entry) {
  346. if (spidev->dev.devt == inode->i_rdev) {
  347. status = 0;
  348. break;
  349. }
  350. }
  351. if (status == 0) {
  352. if (!spidev->buffer) {
  353. spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
  354. if (!spidev->buffer) {
  355. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  356. status = -ENOMEM;
  357. }
  358. }
  359. if (status == 0) {
  360. spidev->users++;
  361. filp->private_data = spidev;
  362. nonseekable_open(inode, filp);
  363. }
  364. } else
  365. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  366. mutex_unlock(&device_list_lock);
  367. return status;
  368. }
  369. static int spidev_release(struct inode *inode, struct file *filp)
  370. {
  371. struct spidev_data *spidev;
  372. int status = 0;
  373. mutex_lock(&device_list_lock);
  374. spidev = filp->private_data;
  375. filp->private_data = NULL;
  376. spidev->users--;
  377. if (!spidev->users) {
  378. kfree(spidev->buffer);
  379. spidev->buffer = NULL;
  380. }
  381. mutex_unlock(&device_list_lock);
  382. return status;
  383. }
  384. static struct file_operations spidev_fops = {
  385. .owner = THIS_MODULE,
  386. /* REVISIT switch to aio primitives, so that userspace
  387. * gets more complete API coverage. It'll simplify things
  388. * too, except for the locking.
  389. */
  390. .write = spidev_write,
  391. .read = spidev_read,
  392. .ioctl = spidev_ioctl,
  393. .open = spidev_open,
  394. .release = spidev_release,
  395. };
  396. /*-------------------------------------------------------------------------*/
  397. /* The main reason to have this class is to make mdev/udev create the
  398. * /dev/spidevB.C character device nodes exposing our userspace API.
  399. * It also simplifies memory management.
  400. */
  401. static void spidev_classdev_release(struct device *dev)
  402. {
  403. struct spidev_data *spidev;
  404. spidev = container_of(dev, struct spidev_data, dev);
  405. kfree(spidev);
  406. }
  407. static struct class spidev_class = {
  408. .name = "spidev",
  409. .owner = THIS_MODULE,
  410. .dev_release = spidev_classdev_release,
  411. };
  412. /*-------------------------------------------------------------------------*/
  413. static int spidev_probe(struct spi_device *spi)
  414. {
  415. struct spidev_data *spidev;
  416. int status;
  417. unsigned long minor;
  418. /* Allocate driver data */
  419. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  420. if (!spidev)
  421. return -ENOMEM;
  422. /* Initialize the driver data */
  423. spidev->spi = spi;
  424. mutex_init(&spidev->buf_lock);
  425. INIT_LIST_HEAD(&spidev->device_entry);
  426. /* If we can allocate a minor number, hook up this device.
  427. * Reusing minors is fine so long as udev or mdev is working.
  428. */
  429. mutex_lock(&device_list_lock);
  430. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  431. if (minor < N_SPI_MINORS) {
  432. spidev->dev.parent = &spi->dev;
  433. spidev->dev.class = &spidev_class;
  434. spidev->dev.devt = MKDEV(SPIDEV_MAJOR, minor);
  435. snprintf(spidev->dev.bus_id, sizeof spidev->dev.bus_id,
  436. "spidev%d.%d",
  437. spi->master->bus_num, spi->chip_select);
  438. status = device_register(&spidev->dev);
  439. } else {
  440. dev_dbg(&spi->dev, "no minor number available!\n");
  441. status = -ENODEV;
  442. }
  443. if (status == 0) {
  444. set_bit(minor, minors);
  445. dev_set_drvdata(&spi->dev, spidev);
  446. list_add(&spidev->device_entry, &device_list);
  447. }
  448. mutex_unlock(&device_list_lock);
  449. if (status != 0)
  450. kfree(spidev);
  451. return status;
  452. }
  453. static int spidev_remove(struct spi_device *spi)
  454. {
  455. struct spidev_data *spidev = dev_get_drvdata(&spi->dev);
  456. mutex_lock(&device_list_lock);
  457. list_del(&spidev->device_entry);
  458. dev_set_drvdata(&spi->dev, NULL);
  459. clear_bit(MINOR(spidev->dev.devt), minors);
  460. device_unregister(&spidev->dev);
  461. mutex_unlock(&device_list_lock);
  462. return 0;
  463. }
  464. static struct spi_driver spidev_spi = {
  465. .driver = {
  466. .name = "spidev",
  467. .owner = THIS_MODULE,
  468. },
  469. .probe = spidev_probe,
  470. .remove = __devexit_p(spidev_remove),
  471. /* NOTE: suspend/resume methods are not necessary here.
  472. * We don't do anything except pass the requests to/from
  473. * the underlying controller. The refrigerator handles
  474. * most issues; the controller driver handles the rest.
  475. */
  476. };
  477. /*-------------------------------------------------------------------------*/
  478. static int __init spidev_init(void)
  479. {
  480. int status;
  481. /* Claim our 256 reserved device numbers. Then register a class
  482. * that will key udev/mdev to add/remove /dev nodes. Last, register
  483. * the driver which manages those device numbers.
  484. */
  485. BUILD_BUG_ON(N_SPI_MINORS > 256);
  486. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  487. if (status < 0)
  488. return status;
  489. status = class_register(&spidev_class);
  490. if (status < 0) {
  491. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  492. return status;
  493. }
  494. status = spi_register_driver(&spidev_spi);
  495. if (status < 0) {
  496. class_unregister(&spidev_class);
  497. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  498. }
  499. return status;
  500. }
  501. module_init(spidev_init);
  502. static void __exit spidev_exit(void)
  503. {
  504. spi_unregister_driver(&spidev_spi);
  505. class_unregister(&spidev_class);
  506. unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
  507. }
  508. module_exit(spidev_exit);
  509. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  510. MODULE_DESCRIPTION("User mode SPI device interface");
  511. MODULE_LICENSE("GPL");