ssegment.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/cdev.h>
  5. #include <linux/slab.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/device.h>
  8. #include <linux/stat.h>
  9. #include <linux/sched.h>
  10. #include <linux/timer.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include "gpioregs.h"
  15. #define DEVICE_NAME "ssegment"
  16. #define CLASS_NAME "ssegment_class"
  17. #define DRIVER_NAME "port80_seg7"
  18. #define BUFFER_SIZE 4
  19. static int major = 56;
  20. static int minor = 40;
  21. module_param(major, int, S_IRUGO);
  22. module_param(minor, int, S_IRUGO);
  23. MODULE_PARM_DESC(major, "Major device number");
  24. MODULE_PARM_DESC(minor, "Minor device number");
  25. static struct class *char_class = NULL;
  26. static struct device *char_device = NULL;
  27. // static struct cdev my_cdev;
  28. static dev_t dev_num;
  29. struct ssegment_dev
  30. {
  31. unsigned char *buffer;
  32. size_t size;
  33. struct mutex lock;
  34. struct cdev cdev;
  35. struct delayed_work delay_work1;
  36. };
  37. static struct ssegment_dev *dev = NULL;
  38. static int wait_ibf(void)
  39. {
  40. int i = 0;
  41. while (inb(EC_CMD_PORT) & EC_IBF)
  42. {
  43. if (++i > TIMEOUT_LOOPS)
  44. {
  45. return -1;
  46. }
  47. udelay(1);
  48. }
  49. return 0;
  50. }
  51. static int wait_obf(void)
  52. {
  53. int i = 0;
  54. while (!(inb(EC_CMD_PORT) & EC_OBF))
  55. {
  56. if (++i > TIMEOUT_LOOPS)
  57. {
  58. return -1;
  59. }
  60. udelay(1);
  61. }
  62. return 0;
  63. }
  64. #if 0
  65. static int ec_read_ram(uint8_t offset, uint8_t *data)
  66. {
  67. if (wait_ibf() < 0)
  68. return -1;
  69. outb(CMD_READ_RAM, EC_CMD_PORT);
  70. if (wait_ibf() < 0)
  71. return -1;
  72. outb(offset, EC_DATA_PORT);
  73. if (wait_obf() < 0)
  74. return -1;
  75. *data = inb(EC_DATA_PORT);
  76. return 0;
  77. }
  78. static int ec_write_ram(uint8_t offset, uint8_t data)
  79. {
  80. if (wait_ibf() < 0)
  81. return -1;
  82. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  83. if (wait_ibf() < 0)
  84. return -1;
  85. outb(offset, EC_DATA_PORT);
  86. if (wait_ibf() < 0)
  87. return -1;
  88. outb(data, EC_DATA_PORT);
  89. return 0;
  90. }
  91. #endif
  92. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  93. {
  94. unsigned char WEC, REC;
  95. switch(page)
  96. {
  97. case 0:
  98. {
  99. WEC = 0x96;
  100. REC = 0x95;
  101. break;
  102. }
  103. case 1:
  104. {
  105. WEC = 0x98;
  106. REC = 0x97;
  107. break;
  108. }
  109. default:
  110. {
  111. WEC = 0x81;
  112. REC = 0x80;
  113. break;
  114. }
  115. }
  116. if (wait_ibf() < 0)
  117. return -1;
  118. outb(REC, EC_CMD_PORT);
  119. if (wait_ibf() < 0)
  120. return -1;
  121. outb(offset, EC_DATA_PORT);
  122. if (wait_obf() < 0)
  123. return -1;
  124. *data = inb(EC_DATA_PORT);
  125. return 0;
  126. }
  127. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  128. {
  129. unsigned char WEC, REC;
  130. switch(page)
  131. {
  132. case 0:
  133. {
  134. WEC = 0x96;
  135. REC = 0x95;
  136. break;
  137. }
  138. case 1:
  139. {
  140. WEC = 0x98;
  141. REC = 0x97;
  142. break;
  143. }
  144. default:
  145. {
  146. WEC = 0x81;
  147. REC = 0x80;
  148. break;
  149. }
  150. }
  151. if (wait_ibf() < 0)
  152. return -1;
  153. outb(WEC, EC_CMD_PORT);
  154. if (wait_ibf() < 0)
  155. return -1;
  156. outb(offset, EC_DATA_PORT);
  157. if (wait_ibf() < 0)
  158. return -1;
  159. outb(data, EC_DATA_PORT);
  160. return 0;
  161. }
  162. unsigned int readl_cust(unsigned int addr)
  163. {
  164. unsigned int val;
  165. void __iomem *reg_base;
  166. reg_base = ioremap(addr, 4);
  167. val = readl(reg_base);
  168. iounmap(reg_base);
  169. return val;
  170. }
  171. int writel_cust(unsigned int addr, unsigned int val)
  172. {
  173. void __iomem *reg_base;
  174. reg_base = ioremap(addr, 4);
  175. writel(val, reg_base);
  176. iounmap(reg_base);
  177. return 0;
  178. }
  179. static void delay_work_func(struct work_struct *work)
  180. {
  181. int ret = 0;
  182. unsigned int val = 0;
  183. unsigned char display_value = 0;
  184. static unsigned int flag = 0;
  185. if(dev->size != 0)
  186. {
  187. if((dev->buffer[1] & 0x80))
  188. {
  189. // blink
  190. flag++;
  191. if(flag % 2 == 1)
  192. {
  193. // blink on
  194. if((dev->buffer[1] & 0x02))
  195. {
  196. val = readl_cust(SSEGMENT_POINT);
  197. val = val | 0x01;
  198. ret = writel_cust(SSEGMENT_POINT, val);
  199. }
  200. else
  201. {
  202. val = readl_cust(SSEGMENT_POINT);
  203. val = val & 0xFFFFFFFE;
  204. ret = writel_cust(SSEGMENT_POINT, val);
  205. }
  206. display_value = dev->buffer[0];
  207. outb(display_value, PORT_80);
  208. oem_ec_write_ram(1, 0x06, 0);
  209. }
  210. else
  211. {
  212. // blink off
  213. display_value = 0;
  214. // off point
  215. val = readl_cust(SSEGMENT_POINT);
  216. val = val & 0xFFFFFFFE;
  217. ret = writel_cust(SSEGMENT_POINT, val);
  218. // off segment
  219. oem_ec_write_ram(1, 0x06, 1);
  220. }
  221. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(500));
  222. }
  223. else
  224. {
  225. if((dev->buffer[1] & 0x02))
  226. {
  227. val = readl_cust(SSEGMENT_POINT);
  228. val = val | 0x01;
  229. ret = writel_cust(SSEGMENT_POINT, val);
  230. }
  231. else
  232. {
  233. val = readl_cust(SSEGMENT_POINT);
  234. val = val & 0xFFFFFFFE;
  235. ret = writel_cust(SSEGMENT_POINT, val);
  236. }
  237. flag = 0;
  238. display_value = dev->buffer[0];
  239. outb(display_value, PORT_80);
  240. }
  241. }
  242. printk(KERN_INFO "delay_work_func\n");
  243. }
  244. static int ssegment_open(struct inode *inode, struct file *filp)
  245. {
  246. struct ssegment_dev *dev;
  247. // if (!request_region(PORT_80, 1, DRIVER_NAME))
  248. // {
  249. // pr_err("Port 80 I/O region busy\n");
  250. // return -EBUSY;
  251. // }
  252. dev = container_of(inode->i_cdev, struct ssegment_dev, cdev);
  253. filp->private_data = dev;
  254. printk(KERN_INFO "ssegment: Device opened (major=%d, minor=%d)\n",
  255. imajor(inode), iminor(inode));
  256. return 0;
  257. }
  258. static int ssegment_release(struct inode *inode, struct file *filp)
  259. {
  260. // release_region(PORT_80, 1);
  261. printk(KERN_INFO "ssegment: Device closed\n");
  262. return 0;
  263. }
  264. static ssize_t ssegment_read(struct file *filp, char __user *buf,
  265. size_t count, loff_t *f_pos)
  266. {
  267. struct ssegment_dev *dev = filp->private_data;
  268. // ssize_t retval = 0;
  269. // size_t available;
  270. int read_count = 0;
  271. int ret = 0;
  272. if (mutex_lock_interruptible(&dev->lock))
  273. return -ERESTARTSYS;
  274. if (count > dev->size)
  275. {
  276. read_count = dev->size;
  277. }
  278. else
  279. {
  280. read_count = count;
  281. }
  282. ret = copy_to_user(buf, dev->buffer, read_count);
  283. if (ret != 0)
  284. {
  285. printk(KERN_INFO "ssegment: copy_to_user failed\n");
  286. goto out;
  287. }
  288. printk(KERN_INFO "ssegment: Read %zu bytes\n", count);
  289. out:
  290. mutex_unlock(&dev->lock);
  291. return ret;
  292. }
  293. static ssize_t ssegment_write(struct file *filp, const char __user *buf,
  294. size_t count, loff_t *f_pos)
  295. {
  296. struct ssegment_dev *dev = filp->private_data;
  297. // ssize_t retval = 0;
  298. // size_t available;
  299. int ret = 0;
  300. if (mutex_lock_interruptible(&dev->lock))
  301. {
  302. return -ERESTARTSYS;
  303. }
  304. if (count > BUFFER_SIZE)
  305. {
  306. count = BUFFER_SIZE;
  307. }
  308. ret = copy_from_user(dev->buffer, buf, count);
  309. if (ret != 0)
  310. {
  311. printk(KERN_INFO "ssegment: copy_from_user failed\n");
  312. goto out;
  313. }
  314. printk("%02x %02x %02x %02x\n", dev->buffer[0], dev->buffer[1], dev->buffer[2], dev->buffer[3]);
  315. dev->size = count;
  316. printk(KERN_INFO "ssegment: Written %zu bytes\n", count);
  317. out:
  318. mutex_unlock(&dev->lock);
  319. if (ret == 0)
  320. {
  321. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(10));
  322. }
  323. return count;
  324. }
  325. static struct file_operations fops = {
  326. .owner = THIS_MODULE,
  327. .open = ssegment_open,
  328. .release = ssegment_release,
  329. .read = ssegment_read,
  330. .write = ssegment_write,
  331. };
  332. static char *my_devnode(struct device *dev, umode_t *mode) {
  333. if (mode) {
  334. *mode = 0666;
  335. }
  336. return NULL;
  337. }
  338. int ssegment_init(void)
  339. {
  340. int result;
  341. printk(KERN_INFO "ssegment: Initializing driver with major=%d, minor=%d\n",
  342. major, minor);
  343. if (major <= 0)
  344. {
  345. printk(KERN_ALERT "ssegment: Invalid major number %d\n", major);
  346. return -EINVAL;
  347. }
  348. dev_num = MKDEV(major, minor);
  349. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  350. if (result < 0)
  351. {
  352. printk(KERN_ALERT "ssegment: Failed to register major number %d\n", major);
  353. printk(KERN_ALERT "ssegment: Try using a different major number\n");
  354. return result;
  355. }
  356. printk(KERN_INFO "ssegment: Registered with major=%d, minor=%d\n",
  357. MAJOR(dev_num), MINOR(dev_num));
  358. dev = kmalloc(sizeof(struct ssegment_dev), GFP_KERNEL);
  359. if (!dev)
  360. {
  361. result = -ENOMEM;
  362. goto fail_malloc;
  363. }
  364. memset(dev, 0, sizeof(struct ssegment_dev));
  365. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  366. if (!dev->buffer)
  367. {
  368. result = -ENOMEM;
  369. goto fail_buffer;
  370. }
  371. dev->buffer[0] = inb(PORT_80);
  372. mutex_init(&dev->lock);
  373. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  374. cdev_init(&dev->cdev, &fops);
  375. dev->cdev.owner = THIS_MODULE;
  376. result = cdev_add(&dev->cdev, dev_num, 1);
  377. if (result)
  378. {
  379. printk(KERN_ALERT "ssegment: Failed to add cdev\n");
  380. goto fail_cdev;
  381. }
  382. char_class = class_create(THIS_MODULE, CLASS_NAME);
  383. if (IS_ERR(char_class))
  384. {
  385. result = PTR_ERR(char_class);
  386. printk(KERN_ALERT "ssegment: Failed to create class\n");
  387. goto fail_class;
  388. }
  389. char_class->devnode = my_devnode;
  390. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  391. if (IS_ERR(char_device))
  392. {
  393. result = PTR_ERR(char_device);
  394. printk(KERN_ALERT "ssegment: Failed to create device\n");
  395. goto fail_device;
  396. }
  397. printk(KERN_INFO "ssegment: Driver initialized successfully\n");
  398. printk(KERN_INFO "ssegment: Device node: /dev/%s (major=%d, minor=%d)\n",
  399. DEVICE_NAME, major, minor);
  400. return 0;
  401. fail_device:
  402. class_destroy(char_class);
  403. fail_class:
  404. cdev_del(&dev->cdev);
  405. fail_cdev:
  406. kfree(dev->buffer);
  407. fail_buffer:
  408. kfree(dev);
  409. fail_malloc:
  410. unregister_chrdev_region(dev_num, 1);
  411. return result;
  412. }
  413. void ssegment_exit(void)
  414. {
  415. cancel_delayed_work_sync(&dev->delay_work1);
  416. device_destroy(char_class, dev_num);
  417. class_destroy(char_class);
  418. if (dev)
  419. {
  420. cdev_del(&dev->cdev);
  421. if (dev->buffer)
  422. kfree(dev->buffer);
  423. kfree(dev);
  424. }
  425. unregister_chrdev_region(dev_num, 1);
  426. printk(KERN_INFO "ssegment: Driver removed (major=%d, minor=%d)\n",
  427. major, minor);
  428. }