landisk_pwb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * arch/sh/boards/landisk/landisk_pwb.c -- driver for the Power control switch.
  3. *
  4. * This driver will also support the I-O DATA Device, Inc. LANDISK Board.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copylight (C) 2002 Atom Create Engineering Co., Ltd.
  11. *
  12. * LED control drive function added by kogiidena
  13. */
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/signal.h>
  17. #include <linux/major.h>
  18. #include <linux/poll.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/timer.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/system.h>
  25. #include <asm/io.h>
  26. #include <asm/irq.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/landisk/iodata_landisk.h>
  29. #define SHUTDOWN_BTN_MINOR 1 /* Shutdown button device minor no. */
  30. #define LED_MINOR 21 /* LED minor no. */
  31. #define BTN_MINOR 22 /* BUTTON minor no. */
  32. #define GIO_MINOR 40 /* GIO minor no. */
  33. static int openCnt;
  34. static int openCntLED;
  35. static int openCntGio;
  36. static int openCntBtn;
  37. static int landisk_btn;
  38. static int landisk_btnctrlpid;
  39. /*
  40. * Functions prototypes
  41. */
  42. static int gio_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  43. unsigned long arg);
  44. static int swdrv_open(struct inode *inode, struct file *filp)
  45. {
  46. int minor;
  47. minor = MINOR(inode->i_rdev);
  48. filp->private_data = (void *)minor;
  49. if (minor == SHUTDOWN_BTN_MINOR) {
  50. if (openCnt > 0) {
  51. return -EALREADY;
  52. } else {
  53. openCnt++;
  54. return 0;
  55. }
  56. } else if (minor == LED_MINOR) {
  57. if (openCntLED > 0) {
  58. return -EALREADY;
  59. } else {
  60. openCntLED++;
  61. return 0;
  62. }
  63. } else if (minor == BTN_MINOR) {
  64. if (openCntBtn > 0) {
  65. return -EALREADY;
  66. } else {
  67. openCntBtn++;
  68. return 0;
  69. }
  70. } else if (minor == GIO_MINOR) {
  71. if (openCntGio > 0) {
  72. return -EALREADY;
  73. } else {
  74. openCntGio++;
  75. return 0;
  76. }
  77. }
  78. return -ENOENT;
  79. }
  80. static int swdrv_close(struct inode *inode, struct file *filp)
  81. {
  82. int minor;
  83. minor = MINOR(inode->i_rdev);
  84. if (minor == SHUTDOWN_BTN_MINOR) {
  85. openCnt--;
  86. } else if (minor == LED_MINOR) {
  87. openCntLED--;
  88. } else if (minor == BTN_MINOR) {
  89. openCntBtn--;
  90. } else if (minor == GIO_MINOR) {
  91. openCntGio--;
  92. }
  93. return 0;
  94. }
  95. static int swdrv_read(struct file *filp, char *buff, size_t count,
  96. loff_t * ppos)
  97. {
  98. int minor;
  99. minor = (int)(filp->private_data);
  100. if (!access_ok(VERIFY_WRITE, (void *)buff, count))
  101. return -EFAULT;
  102. if (minor == SHUTDOWN_BTN_MINOR) {
  103. if (landisk_btn & 0x10) {
  104. put_user(1, buff);
  105. return 1;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. return 0;
  111. }
  112. static int swdrv_write(struct file *filp, const char *buff, size_t count,
  113. loff_t * ppos)
  114. {
  115. int minor;
  116. minor = (int)(filp->private_data);
  117. if (minor == SHUTDOWN_BTN_MINOR) {
  118. return count;
  119. }
  120. return count;
  121. }
  122. static irqreturn_t sw_interrupt(int irq, void *dev_id)
  123. {
  124. landisk_btn = (0x0ff & (~ctrl_inb(PA_STATUS)));
  125. disable_irq(IRQ_BUTTON);
  126. disable_irq(IRQ_POWER);
  127. ctrl_outb(0x00, PA_PWRINT_CLR);
  128. if (landisk_btnctrlpid != 0) {
  129. kill_proc(landisk_btnctrlpid, SIGUSR1, 1);
  130. landisk_btnctrlpid = 0;
  131. }
  132. return IRQ_HANDLED;
  133. }
  134. static struct file_operations swdrv_fops = {
  135. .read = swdrv_read, /* read */
  136. .write = swdrv_write, /* write */
  137. .open = swdrv_open, /* open */
  138. .release = swdrv_close, /* release */
  139. .ioctl = gio_ioctl, /* ioctl */
  140. };
  141. static char banner[] __initdata =
  142. KERN_INFO "LANDISK and USL-5P Button, LED and GIO driver initialized\n";
  143. int __init swdrv_init(void)
  144. {
  145. int error;
  146. printk("%s", banner);
  147. openCnt = 0;
  148. openCntLED = 0;
  149. openCntBtn = 0;
  150. openCntGio = 0;
  151. landisk_btn = 0;
  152. landisk_btnctrlpid = 0;
  153. if ((error = register_chrdev(SHUTDOWN_BTN_MAJOR, "swdrv", &swdrv_fops))) {
  154. printk(KERN_ERR
  155. "Button, LED and GIO driver:Couldn't register driver, error=%d\n",
  156. error);
  157. return 1;
  158. }
  159. if (request_irq(IRQ_POWER, sw_interrupt, 0, "SHUTDOWNSWITCH", NULL)) {
  160. printk(KERN_ERR "Unable to get IRQ 11.\n");
  161. return 1;
  162. }
  163. if (request_irq(IRQ_BUTTON, sw_interrupt, 0, "USL-5P BUTTON", NULL)) {
  164. printk(KERN_ERR "Unable to get IRQ 12.\n");
  165. return 1;
  166. }
  167. ctrl_outb(0x00, PA_PWRINT_CLR);
  168. return 0;
  169. }
  170. module_init(swdrv_init);
  171. /*
  172. * gio driver
  173. *
  174. */
  175. #include <asm/landisk/gio.h>
  176. static int gio_ioctl(struct inode *inode, struct file *filp,
  177. unsigned int cmd, unsigned long arg)
  178. {
  179. int minor;
  180. unsigned int data, mask;
  181. static unsigned int addr = 0;
  182. minor = (int)(filp->private_data);
  183. /* access control */
  184. if (minor == GIO_MINOR) {
  185. ;
  186. } else if (minor == LED_MINOR) {
  187. if (((cmd & 0x0ff) >= 9) && ((cmd & 0x0ff) < 20)) {
  188. ;
  189. } else {
  190. return -EINVAL;
  191. }
  192. } else if (minor == BTN_MINOR) {
  193. if (((cmd & 0x0ff) >= 20) && ((cmd & 0x0ff) < 30)) {
  194. ;
  195. } else {
  196. return -EINVAL;
  197. }
  198. } else {
  199. return -EINVAL;
  200. }
  201. if (cmd & 0x01) { /* write */
  202. if (copy_from_user(&data, (int *)arg, sizeof(int))) {
  203. return -EFAULT;
  204. }
  205. }
  206. switch (cmd) {
  207. case GIODRV_IOCSGIOSETADDR: /* addres set */
  208. addr = data;
  209. break;
  210. case GIODRV_IOCSGIODATA1: /* write byte */
  211. ctrl_outb((unsigned char)(0x0ff & data), addr);
  212. break;
  213. case GIODRV_IOCSGIODATA2: /* write word */
  214. if (addr & 0x01) {
  215. return -EFAULT;
  216. }
  217. ctrl_outw((unsigned short int)(0x0ffff & data), addr);
  218. break;
  219. case GIODRV_IOCSGIODATA4: /* write long */
  220. if (addr & 0x03) {
  221. return -EFAULT;
  222. }
  223. ctrl_outl(data, addr);
  224. break;
  225. case GIODRV_IOCGGIODATA1: /* read byte */
  226. data = ctrl_inb(addr);
  227. break;
  228. case GIODRV_IOCGGIODATA2: /* read word */
  229. if (addr & 0x01) {
  230. return -EFAULT;
  231. }
  232. data = ctrl_inw(addr);
  233. break;
  234. case GIODRV_IOCGGIODATA4: /* read long */
  235. if (addr & 0x03) {
  236. return -EFAULT;
  237. }
  238. data = ctrl_inl(addr);
  239. break;
  240. case GIODRV_IOCSGIO_LED: /* write */
  241. mask = ((data & 0x00ffffff) << 8)
  242. | ((data & 0x0000ffff) << 16)
  243. | ((data & 0x000000ff) << 24);
  244. landisk_ledparam = data & (~mask);
  245. if (landisk_arch == 0) { /* arch == landisk */
  246. landisk_ledparam &= 0x03030303;
  247. mask = (~(landisk_ledparam >> 22)) & 0x000c;
  248. landisk_ledparam |= mask;
  249. } else { /* arch == usl-5p */
  250. mask = (landisk_ledparam >> 24) & 0x0001;
  251. landisk_ledparam |= mask;
  252. landisk_ledparam &= 0x007f7f7f;
  253. }
  254. landisk_ledparam |= 0x80;
  255. break;
  256. case GIODRV_IOCGGIO_LED: /* read */
  257. data = landisk_ledparam;
  258. if (landisk_arch == 0) { /* arch == landisk */
  259. data &= 0x03030303;
  260. } else { /* arch == usl-5p */
  261. ;
  262. }
  263. data &= (~0x080);
  264. break;
  265. case GIODRV_IOCSGIO_BUZZER: /* write */
  266. landisk_buzzerparam = data;
  267. landisk_ledparam |= 0x80;
  268. break;
  269. case GIODRV_IOCGGIO_LANDISK: /* read */
  270. data = landisk_arch & 0x01;
  271. break;
  272. case GIODRV_IOCGGIO_BTN: /* read */
  273. data = (0x0ff & ctrl_inb(PA_PWRINT_CLR));
  274. data <<= 8;
  275. data |= (0x0ff & ctrl_inb(PA_IMASK));
  276. data <<= 8;
  277. data |= (0x0ff & landisk_btn);
  278. data <<= 8;
  279. data |= (0x0ff & (~ctrl_inb(PA_STATUS)));
  280. break;
  281. case GIODRV_IOCSGIO_BTNPID: /* write */
  282. landisk_btnctrlpid = data;
  283. landisk_btn = 0;
  284. if (irq_desc[IRQ_BUTTON].depth) {
  285. enable_irq(IRQ_BUTTON);
  286. }
  287. if (irq_desc[IRQ_POWER].depth) {
  288. enable_irq(IRQ_POWER);
  289. }
  290. break;
  291. case GIODRV_IOCGGIO_BTNPID: /* read */
  292. data = landisk_btnctrlpid;
  293. break;
  294. default:
  295. return -EFAULT;
  296. break;
  297. }
  298. if ((cmd & 0x01) == 0) { /* read */
  299. if (copy_to_user((int *)arg, &data, sizeof(int))) {
  300. return -EFAULT;
  301. }
  302. }
  303. return 0;
  304. }