spidev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/err.h>
  28. #include <linux/list.h>
  29. #include <linux/errno.h>
  30. #include <linux/mutex.h>
  31. #include <linux/slab.h>
  32. #include <linux/compat.h>
  33. #include <linux/spi/spi.h>
  34. #include <linux/spi/spidev.h>
  35. #include <asm/uaccess.h>
  36. /*
  37. * This supports access to SPI devices using normal userspace I/O calls.
  38. * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
  39. * and often mask message boundaries, full SPI support requires full duplex
  40. * transfers. There are several kinds of internal message boundaries to
  41. * handle chipselect management and other protocol options.
  42. *
  43. * SPI has a character major number assigned. We allocate minor numbers
  44. * dynamically using a bitmask. You must use hotplug tools, such as udev
  45. * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
  46. * nodes, since there is no fixed association of minor numbers with any
  47. * particular SPI bus or device.
  48. */
  49. #define SPIDEV_MAJOR 153 /* assigned */
  50. #define N_SPI_MINORS 32 /* ... up to 256 */
  51. static DECLARE_BITMAP(minors, N_SPI_MINORS);
  52. /* Bit masks for spi_device.mode management. Note that incorrect
  53. * settings for some settings can cause *lots* of trouble for other
  54. * devices on a shared bus:
  55. *
  56. * - CS_HIGH ... this device will be active when it shouldn't be
  57. * - 3WIRE ... when active, it won't behave as it should
  58. * - NO_CS ... there will be no explicit message boundaries; this
  59. * is completely incompatible with the shared bus model
  60. * - READY ... transfers may proceed when they shouldn't.
  61. *
  62. * REVISIT should changing those flags be privileged?
  63. */
  64. #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
  65. | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
  66. | SPI_NO_CS | SPI_READY)
  67. struct spidev_data {
  68. dev_t devt;
  69. spinlock_t spi_lock;
  70. struct spi_device *spi;
  71. struct list_head device_entry;
  72. /* buffer is NULL unless this device is open (users > 0) */
  73. struct mutex buf_lock;
  74. unsigned users;
  75. u8 *buffer;
  76. };
  77. static LIST_HEAD(device_list);
  78. static DEFINE_MUTEX(device_list_lock);
  79. static unsigned bufsiz = 4096;
  80. module_param(bufsiz, uint, S_IRUGO);
  81. MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
  82. /*-------------------------------------------------------------------------*/
  83. /*
  84. * We can't use the standard synchronous wrappers for file I/O; we
  85. * need to protect against async removal of the underlying spi_device.
  86. */
  87. static void spidev_complete(void *arg)
  88. {
  89. complete(arg);
  90. }
  91. static ssize_t
  92. spidev_sync(struct spidev_data *spidev, struct spi_message *message)
  93. {
  94. DECLARE_COMPLETION_ONSTACK(done);
  95. int status;
  96. message->complete = spidev_complete;
  97. message->context = &done;
  98. spin_lock_irq(&spidev->spi_lock);
  99. if (spidev->spi == NULL)
  100. status = -ESHUTDOWN;
  101. else
  102. status = spi_async(spidev->spi, message);
  103. spin_unlock_irq(&spidev->spi_lock);
  104. if (status == 0) {
  105. wait_for_completion(&done);
  106. status = message->status;
  107. if (status == 0)
  108. status = message->actual_length;
  109. }
  110. return status;
  111. }
  112. static inline ssize_t
  113. spidev_sync_write(struct spidev_data *spidev, size_t len)
  114. {
  115. struct spi_transfer t = {
  116. .tx_buf = spidev->buffer,
  117. .len = len,
  118. };
  119. struct spi_message m;
  120. spi_message_init(&m);
  121. spi_message_add_tail(&t, &m);
  122. return spidev_sync(spidev, &m);
  123. }
  124. static inline ssize_t
  125. spidev_sync_read(struct spidev_data *spidev, size_t len)
  126. {
  127. struct spi_transfer t = {
  128. .rx_buf = spidev->buffer,
  129. .len = len,
  130. };
  131. struct spi_message m;
  132. spi_message_init(&m);
  133. spi_message_add_tail(&t, &m);
  134. return spidev_sync(spidev, &m);
  135. }
  136. /*-------------------------------------------------------------------------*/
  137. /* Read-only message with current device setup */
  138. static ssize_t
  139. spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  140. {
  141. struct spidev_data *spidev;
  142. ssize_t status = 0;
  143. /* chipselect only toggles at start or end of operation */
  144. if (count > bufsiz)
  145. return -EMSGSIZE;
  146. spidev = filp->private_data;
  147. mutex_lock(&spidev->buf_lock);
  148. status = spidev_sync_read(spidev, count);
  149. if (status > 0) {
  150. unsigned long missing;
  151. missing = copy_to_user(buf, spidev->buffer, status);
  152. if (missing == status)
  153. status = -EFAULT;
  154. else
  155. status = status - missing;
  156. }
  157. mutex_unlock(&spidev->buf_lock);
  158. return status;
  159. }
  160. /* Write-only message with current device setup */
  161. static ssize_t
  162. spidev_write(struct file *filp, const char __user *buf,
  163. size_t count, loff_t *f_pos)
  164. {
  165. struct spidev_data *spidev;
  166. ssize_t status = 0;
  167. unsigned long missing;
  168. /* chipselect only toggles at start or end of operation */
  169. if (count > bufsiz)
  170. return -EMSGSIZE;
  171. spidev = filp->private_data;
  172. mutex_lock(&spidev->buf_lock);
  173. missing = copy_from_user(spidev->buffer, buf, count);
  174. if (missing == 0) {
  175. status = spidev_sync_write(spidev, count);
  176. } else
  177. status = -EFAULT;
  178. mutex_unlock(&spidev->buf_lock);
  179. return status;
  180. }
  181. static int spidev_message(struct spidev_data *spidev,
  182. struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
  183. {
  184. struct spi_message msg;
  185. struct spi_transfer *k_xfers;
  186. struct spi_transfer *k_tmp;
  187. struct spi_ioc_transfer *u_tmp;
  188. unsigned n, total;
  189. u8 *buf;
  190. int status = -EFAULT;
  191. spi_message_init(&msg);
  192. k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
  193. if (k_xfers == NULL)
  194. return -ENOMEM;
  195. /* Construct spi_message, copying any tx data to bounce buffer.
  196. * We walk the array of user-provided transfers, using each one
  197. * to initialize a kernel version of the same transfer.
  198. */
  199. buf = spidev->buffer;
  200. total = 0;
  201. for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  202. n;
  203. n--, k_tmp++, u_tmp++) {
  204. k_tmp->len = u_tmp->len;
  205. total += k_tmp->len;
  206. if (total > bufsiz) {
  207. status = -EMSGSIZE;
  208. goto done;
  209. }
  210. if (u_tmp->rx_buf) {
  211. k_tmp->rx_buf = buf;
  212. if (!access_ok(VERIFY_WRITE, (u8 __user *)
  213. (uintptr_t) u_tmp->rx_buf,
  214. u_tmp->len))
  215. goto done;
  216. }
  217. if (u_tmp->tx_buf) {
  218. k_tmp->tx_buf = buf;
  219. if (copy_from_user(buf, (const u8 __user *)
  220. (uintptr_t) u_tmp->tx_buf,
  221. u_tmp->len))
  222. goto done;
  223. }
  224. buf += k_tmp->len;
  225. k_tmp->cs_change = !!u_tmp->cs_change;
  226. k_tmp->bits_per_word = u_tmp->bits_per_word;
  227. k_tmp->delay_usecs = u_tmp->delay_usecs;
  228. k_tmp->speed_hz = u_tmp->speed_hz;
  229. #ifdef VERBOSE
  230. dev_dbg(&spidev->spi->dev,
  231. " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
  232. u_tmp->len,
  233. u_tmp->rx_buf ? "rx " : "",
  234. u_tmp->tx_buf ? "tx " : "",
  235. u_tmp->cs_change ? "cs " : "",
  236. u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
  237. u_tmp->delay_usecs,
  238. u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
  239. #endif
  240. spi_message_add_tail(k_tmp, &msg);
  241. }
  242. status = spidev_sync(spidev, &msg);
  243. if (status < 0)
  244. goto done;
  245. /* copy any rx data out of bounce buffer */
  246. buf = spidev->buffer;
  247. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  248. if (u_tmp->rx_buf) {
  249. if (__copy_to_user((u8 __user *)
  250. (uintptr_t) u_tmp->rx_buf, buf,
  251. u_tmp->len)) {
  252. status = -EFAULT;
  253. goto done;
  254. }
  255. }
  256. buf += u_tmp->len;
  257. }
  258. status = total;
  259. done:
  260. kfree(k_xfers);
  261. return status;
  262. }
  263. static long
  264. spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  265. {
  266. int err = 0;
  267. int retval = 0;
  268. struct spidev_data *spidev;
  269. struct spi_device *spi;
  270. u32 tmp;
  271. unsigned n_ioc;
  272. struct spi_ioc_transfer *ioc;
  273. /* Check type and command number */
  274. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  275. return -ENOTTY;
  276. /* Check access direction once here; don't repeat below.
  277. * IOC_DIR is from the user perspective, while access_ok is
  278. * from the kernel perspective; so they look reversed.
  279. */
  280. if (_IOC_DIR(cmd) & _IOC_READ)
  281. err = !access_ok(VERIFY_WRITE,
  282. (void __user *)arg, _IOC_SIZE(cmd));
  283. if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  284. err = !access_ok(VERIFY_READ,
  285. (void __user *)arg, _IOC_SIZE(cmd));
  286. if (err)
  287. return -EFAULT;
  288. /* guard against device removal before, or while,
  289. * we issue this ioctl.
  290. */
  291. spidev = filp->private_data;
  292. spin_lock_irq(&spidev->spi_lock);
  293. spi = spi_dev_get(spidev->spi);
  294. spin_unlock_irq(&spidev->spi_lock);
  295. if (spi == NULL)
  296. return -ESHUTDOWN;
  297. /* use the buffer lock here for triple duty:
  298. * - prevent I/O (from us) so calling spi_setup() is safe;
  299. * - prevent concurrent SPI_IOC_WR_* from morphing
  300. * data fields while SPI_IOC_RD_* reads them;
  301. * - SPI_IOC_MESSAGE needs the buffer locked "normally".
  302. */
  303. mutex_lock(&spidev->buf_lock);
  304. switch (cmd) {
  305. /* read requests */
  306. case SPI_IOC_RD_MODE:
  307. retval = __put_user(spi->mode & SPI_MODE_MASK,
  308. (__u8 __user *)arg);
  309. break;
  310. case SPI_IOC_RD_LSB_FIRST:
  311. retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  312. (__u8 __user *)arg);
  313. break;
  314. case SPI_IOC_RD_BITS_PER_WORD:
  315. retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  316. break;
  317. case SPI_IOC_RD_MAX_SPEED_HZ:
  318. retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
  319. break;
  320. /* write requests */
  321. case SPI_IOC_WR_MODE:
  322. retval = __get_user(tmp, (u8 __user *)arg);
  323. if (retval == 0) {
  324. u8 save = spi->mode;
  325. if (tmp & ~SPI_MODE_MASK) {
  326. retval = -EINVAL;
  327. break;
  328. }
  329. tmp |= spi->mode & ~SPI_MODE_MASK;
  330. spi->mode = (u8)tmp;
  331. retval = spi_setup(spi);
  332. if (retval < 0)
  333. spi->mode = save;
  334. else
  335. dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
  336. }
  337. break;
  338. case SPI_IOC_WR_LSB_FIRST:
  339. retval = __get_user(tmp, (__u8 __user *)arg);
  340. if (retval == 0) {
  341. u8 save = spi->mode;
  342. if (tmp)
  343. spi->mode |= SPI_LSB_FIRST;
  344. else
  345. spi->mode &= ~SPI_LSB_FIRST;
  346. retval = spi_setup(spi);
  347. if (retval < 0)
  348. spi->mode = save;
  349. else
  350. dev_dbg(&spi->dev, "%csb first\n",
  351. tmp ? 'l' : 'm');
  352. }
  353. break;
  354. case SPI_IOC_WR_BITS_PER_WORD:
  355. retval = __get_user(tmp, (__u8 __user *)arg);
  356. if (retval == 0) {
  357. u8 save = spi->bits_per_word;
  358. spi->bits_per_word = tmp;
  359. retval = spi_setup(spi);
  360. if (retval < 0)
  361. spi->bits_per_word = save;
  362. else
  363. dev_dbg(&spi->dev, "%d bits per word\n", tmp);
  364. }
  365. break;
  366. case SPI_IOC_WR_MAX_SPEED_HZ:
  367. retval = __get_user(tmp, (__u32 __user *)arg);
  368. if (retval == 0) {
  369. u32 save = spi->max_speed_hz;
  370. spi->max_speed_hz = tmp;
  371. retval = spi_setup(spi);
  372. if (retval < 0)
  373. spi->max_speed_hz = save;
  374. else
  375. dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
  376. }
  377. break;
  378. default:
  379. /* segmented and/or full-duplex I/O request */
  380. if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  381. || _IOC_DIR(cmd) != _IOC_WRITE) {
  382. retval = -ENOTTY;
  383. break;
  384. }
  385. tmp = _IOC_SIZE(cmd);
  386. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
  387. retval = -EINVAL;
  388. break;
  389. }
  390. n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  391. if (n_ioc == 0)
  392. break;
  393. /* copy into scratch area */
  394. ioc = kmalloc(tmp, GFP_KERNEL);
  395. if (!ioc) {
  396. retval = -ENOMEM;
  397. break;
  398. }
  399. if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
  400. kfree(ioc);
  401. retval = -EFAULT;
  402. break;
  403. }
  404. /* translate to spi_message, execute */
  405. retval = spidev_message(spidev, ioc, n_ioc);
  406. kfree(ioc);
  407. break;
  408. }
  409. mutex_unlock(&spidev->buf_lock);
  410. spi_dev_put(spi);
  411. return retval;
  412. }
  413. #ifdef CONFIG_COMPAT
  414. static long
  415. spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  416. {
  417. return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  418. }
  419. #else
  420. #define spidev_compat_ioctl NULL
  421. #endif /* CONFIG_COMPAT */
  422. static int spidev_open(struct inode *inode, struct file *filp)
  423. {
  424. struct spidev_data *spidev;
  425. int status = -ENXIO;
  426. mutex_lock(&device_list_lock);
  427. list_for_each_entry(spidev, &device_list, device_entry) {
  428. if (spidev->devt == inode->i_rdev) {
  429. status = 0;
  430. break;
  431. }
  432. }
  433. if (status == 0) {
  434. if (!spidev->buffer) {
  435. spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
  436. if (!spidev->buffer) {
  437. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  438. status = -ENOMEM;
  439. }
  440. }
  441. if (status == 0) {
  442. spidev->users++;
  443. filp->private_data = spidev;
  444. nonseekable_open(inode, filp);
  445. }
  446. } else
  447. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  448. mutex_unlock(&device_list_lock);
  449. return status;
  450. }
  451. static int spidev_release(struct inode *inode, struct file *filp)
  452. {
  453. struct spidev_data *spidev;
  454. int status = 0;
  455. mutex_lock(&device_list_lock);
  456. spidev = filp->private_data;
  457. filp->private_data = NULL;
  458. /* last close? */
  459. spidev->users--;
  460. if (!spidev->users) {
  461. int dofree;
  462. kfree(spidev->buffer);
  463. spidev->buffer = NULL;
  464. /* ... after we unbound from the underlying device? */
  465. spin_lock_irq(&spidev->spi_lock);
  466. dofree = (spidev->spi == NULL);
  467. spin_unlock_irq(&spidev->spi_lock);
  468. if (dofree)
  469. kfree(spidev);
  470. }
  471. mutex_unlock(&device_list_lock);
  472. return status;
  473. }
  474. static const struct file_operations spidev_fops = {
  475. .owner = THIS_MODULE,
  476. /* REVISIT switch to aio primitives, so that userspace
  477. * gets more complete API coverage. It'll simplify things
  478. * too, except for the locking.
  479. */
  480. .write = spidev_write,
  481. .read = spidev_read,
  482. .unlocked_ioctl = spidev_ioctl,
  483. .compat_ioctl = spidev_compat_ioctl,
  484. .open = spidev_open,
  485. .release = spidev_release,
  486. .llseek = no_llseek,
  487. };
  488. /*-------------------------------------------------------------------------*/
  489. /* The main reason to have this class is to make mdev/udev create the
  490. * /dev/spidevB.C character device nodes exposing our userspace API.
  491. * It also simplifies memory management.
  492. */
  493. static struct class *spidev_class;
  494. /*-------------------------------------------------------------------------*/
  495. static int __devinit spidev_probe(struct spi_device *spi)
  496. {
  497. struct spidev_data *spidev;
  498. int status;
  499. unsigned long minor;
  500. /* Allocate driver data */
  501. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  502. if (!spidev)
  503. return -ENOMEM;
  504. /* Initialize the driver data */
  505. spidev->spi = spi;
  506. spin_lock_init(&spidev->spi_lock);
  507. mutex_init(&spidev->buf_lock);
  508. INIT_LIST_HEAD(&spidev->device_entry);
  509. /* If we can allocate a minor number, hook up this device.
  510. * Reusing minors is fine so long as udev or mdev is working.
  511. */
  512. mutex_lock(&device_list_lock);
  513. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  514. if (minor < N_SPI_MINORS) {
  515. struct device *dev;
  516. spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
  517. dev = device_create(spidev_class, &spi->dev, spidev->devt,
  518. spidev, "spidev%d.%d",
  519. spi->master->bus_num, spi->chip_select);
  520. status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
  521. } else {
  522. dev_dbg(&spi->dev, "no minor number available!\n");
  523. status = -ENODEV;
  524. }
  525. if (status == 0) {
  526. set_bit(minor, minors);
  527. list_add(&spidev->device_entry, &device_list);
  528. }
  529. mutex_unlock(&device_list_lock);
  530. if (status == 0)
  531. spi_set_drvdata(spi, spidev);
  532. else
  533. kfree(spidev);
  534. return status;
  535. }
  536. static int __devexit spidev_remove(struct spi_device *spi)
  537. {
  538. struct spidev_data *spidev = spi_get_drvdata(spi);
  539. /* make sure ops on existing fds can abort cleanly */
  540. spin_lock_irq(&spidev->spi_lock);
  541. spidev->spi = NULL;
  542. spi_set_drvdata(spi, NULL);
  543. spin_unlock_irq(&spidev->spi_lock);
  544. /* prevent new opens */
  545. mutex_lock(&device_list_lock);
  546. list_del(&spidev->device_entry);
  547. device_destroy(spidev_class, spidev->devt);
  548. clear_bit(MINOR(spidev->devt), minors);
  549. if (spidev->users == 0)
  550. kfree(spidev);
  551. mutex_unlock(&device_list_lock);
  552. return 0;
  553. }
  554. static struct spi_driver spidev_spi_driver = {
  555. .driver = {
  556. .name = "spidev",
  557. .owner = THIS_MODULE,
  558. },
  559. .probe = spidev_probe,
  560. .remove = __devexit_p(spidev_remove),
  561. /* NOTE: suspend/resume methods are not necessary here.
  562. * We don't do anything except pass the requests to/from
  563. * the underlying controller. The refrigerator handles
  564. * most issues; the controller driver handles the rest.
  565. */
  566. };
  567. /*-------------------------------------------------------------------------*/
  568. static int __init spidev_init(void)
  569. {
  570. int status;
  571. /* Claim our 256 reserved device numbers. Then register a class
  572. * that will key udev/mdev to add/remove /dev nodes. Last, register
  573. * the driver which manages those device numbers.
  574. */
  575. BUILD_BUG_ON(N_SPI_MINORS > 256);
  576. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  577. if (status < 0)
  578. return status;
  579. spidev_class = class_create(THIS_MODULE, "spidev");
  580. if (IS_ERR(spidev_class)) {
  581. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  582. return PTR_ERR(spidev_class);
  583. }
  584. status = spi_register_driver(&spidev_spi_driver);
  585. if (status < 0) {
  586. class_destroy(spidev_class);
  587. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  588. }
  589. return status;
  590. }
  591. module_init(spidev_init);
  592. static void __exit spidev_exit(void)
  593. {
  594. spi_unregister_driver(&spidev_spi_driver);
  595. class_destroy(spidev_class);
  596. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  597. }
  598. module_exit(spidev_exit);
  599. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  600. MODULE_DESCRIPTION("User mode SPI device interface");
  601. MODULE_LICENSE("GPL");
  602. MODULE_ALIAS("spi:spidev");