spidev.c 17 KB

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