spidev.c 14 KB

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