switch-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * switch-core.c
  3. *
  4. * Copyright (C) 2005 Felix Fietkau <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. *
  21. * Basic doc of driver's /proc interface:
  22. * /proc/switch/<interface>/
  23. * registers: read-only
  24. * counters: read-only
  25. * reset: write causes hardware reset
  26. * enable_vlan: "0", "1"
  27. * port/<port-number>/
  28. * enabled: "0", "1"
  29. * media: "AUTO", "100FD", "100HD", "10FD", "10HD"
  30. * vlan/<port-number>/
  31. * ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/list.h>
  38. #include "switch-core.h"
  39. static int drv_num = 0;
  40. static struct proc_dir_entry *switch_root;
  41. switch_driver drivers;
  42. typedef struct {
  43. struct list_head list;
  44. struct proc_dir_entry *parent;
  45. int nr;
  46. void *driver;
  47. switch_config handler;
  48. } switch_proc_handler;
  49. typedef struct {
  50. struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
  51. struct proc_dir_entry **ports, **vlans;
  52. switch_proc_handler data;
  53. int nr;
  54. } switch_priv;
  55. static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
  56. static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data);
  57. static struct file_operations switch_proc_fops = {
  58. .read = (ssize_t (*) (struct file *, char __user *, size_t, loff_t *))switch_proc_read,
  59. .write = (ssize_t (*) (struct file *, const char __user *, size_t, loff_t *))switch_proc_write
  60. };
  61. static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
  62. {
  63. struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
  64. char *page;
  65. int len = 0;
  66. if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
  67. return -ENOBUFS;
  68. if (dent->data != NULL) {
  69. switch_proc_handler *handler = (switch_proc_handler *) dent->data;
  70. if (handler->handler.read != NULL)
  71. len += handler->handler.read(handler->driver, page + len, handler->nr);
  72. }
  73. len += 1;
  74. if (*ppos < len) {
  75. len = min_t(int, len - *ppos, count);
  76. if (copy_to_user(buf, (page + *ppos), len)) {
  77. kfree(page);
  78. return -EFAULT;
  79. }
  80. *ppos += len;
  81. } else {
  82. len = 0;
  83. }
  84. kfree(page);
  85. return len;
  86. }
  87. static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
  88. {
  89. struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
  90. char *page;
  91. int ret = -EINVAL;
  92. if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
  93. return -ENOBUFS;
  94. if (copy_from_user(page, buf, count)) {
  95. kfree(page);
  96. return -EINVAL;
  97. }
  98. page[count] = 0;
  99. if (dent->data != NULL) {
  100. switch_proc_handler *handler = (switch_proc_handler *) dent->data;
  101. if (handler->handler.write != NULL) {
  102. if ((ret = handler->handler.write(handler->driver, page, handler->nr)) >= 0)
  103. ret = count;
  104. }
  105. }
  106. kfree(page);
  107. return ret;
  108. }
  109. static int handle_driver_name(void *driver, char *buf, int nr)
  110. {
  111. const char *name = ((switch_driver *) driver)->name;
  112. return sprintf(buf, "%s\n", name);
  113. }
  114. static int handle_driver_version(void *driver, char *buf, int nr)
  115. {
  116. const char *version = ((switch_driver *) driver)->version;
  117. strcpy(buf, version);
  118. return sprintf(buf, "%s\n", version);
  119. }
  120. static void add_handler(switch_driver *driver, const switch_config *handler, struct proc_dir_entry *parent, int nr)
  121. {
  122. switch_priv *priv = (switch_priv *) driver->data;
  123. struct proc_dir_entry *p;
  124. int mode;
  125. switch_proc_handler *tmp;
  126. tmp = (switch_proc_handler *) kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
  127. if (!tmp)
  128. return;
  129. INIT_LIST_HEAD(&tmp->list);
  130. tmp->parent = parent;
  131. tmp->nr = nr;
  132. tmp->driver = driver;
  133. memcpy(&tmp->handler, handler, sizeof(switch_config));
  134. list_add(&tmp->list, &priv->data.list);
  135. mode = 0;
  136. if (handler->read != NULL) mode |= S_IRUSR;
  137. if (handler->write != NULL) mode |= S_IWUSR;
  138. if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
  139. p->data = (void *) tmp;
  140. p->proc_fops = &switch_proc_fops;
  141. }
  142. }
  143. static inline void add_handlers(switch_driver *driver, const switch_config *handlers, struct proc_dir_entry *parent, int nr)
  144. {
  145. int i;
  146. for (i = 0; handlers[i].name != NULL; i++) {
  147. add_handler(driver, &(handlers[i]), parent, nr);
  148. }
  149. }
  150. static void remove_handlers(switch_priv *priv)
  151. {
  152. struct list_head *pos, *q;
  153. switch_proc_handler *tmp;
  154. list_for_each_safe(pos, q, &priv->data.list) {
  155. tmp = list_entry(pos, switch_proc_handler, list);
  156. list_del(pos);
  157. remove_proc_entry(tmp->handler.name, tmp->parent);
  158. kfree(tmp);
  159. }
  160. }
  161. static void do_unregister(switch_driver *driver)
  162. {
  163. char buf[4];
  164. int i;
  165. switch_priv *priv = (switch_priv *) driver->data;
  166. remove_handlers(priv);
  167. for(i = 0; priv->ports[i] != NULL; i++) {
  168. sprintf(buf, "%d", i);
  169. remove_proc_entry(buf, priv->port_dir);
  170. }
  171. kfree(priv->ports);
  172. remove_proc_entry("port", priv->driver_dir);
  173. for(i = 0; priv->vlans[i] != NULL; i++) {
  174. sprintf(buf, "%d", i);
  175. remove_proc_entry(buf, priv->vlan_dir);
  176. }
  177. kfree(priv->vlans);
  178. remove_proc_entry("vlan", priv->driver_dir);
  179. remove_proc_entry(driver->interface, switch_root);
  180. if (priv->nr == (drv_num - 1))
  181. drv_num--;
  182. kfree(priv);
  183. }
  184. switch_config global_driver_handlers[] = {
  185. {"driver", handle_driver_name, NULL},
  186. {"version", handle_driver_version, NULL},
  187. {NULL, NULL, NULL}
  188. };
  189. static int do_register(switch_driver *driver)
  190. {
  191. switch_priv *priv;
  192. int i;
  193. char buf[4];
  194. priv = kmalloc(sizeof(switch_priv), GFP_KERNEL);
  195. if (!priv)
  196. return -ENOMEM;
  197. driver->data = (void *) priv;
  198. priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *),
  199. GFP_KERNEL);
  200. if (!priv->ports) {
  201. kfree(priv);
  202. return -ENOMEM;
  203. }
  204. priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *),
  205. GFP_KERNEL);
  206. if (!priv->vlans) {
  207. kfree(priv->ports);
  208. kfree(priv);
  209. return -ENOMEM;
  210. }
  211. INIT_LIST_HEAD(&priv->data.list);
  212. priv->nr = drv_num++;
  213. priv->driver_dir = proc_mkdir(driver->interface, switch_root);
  214. if (driver->driver_handlers != NULL) {
  215. add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
  216. add_handlers(driver, global_driver_handlers, priv->driver_dir, 0);
  217. }
  218. priv->port_dir = proc_mkdir("port", priv->driver_dir);
  219. for (i = 0; i < driver->ports; i++) {
  220. sprintf(buf, "%d", i);
  221. priv->ports[i] = proc_mkdir(buf, priv->port_dir);
  222. if (driver->port_handlers != NULL)
  223. add_handlers(driver, driver->port_handlers, priv->ports[i], i);
  224. }
  225. priv->ports[i] = NULL;
  226. priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
  227. for (i = 0; i < driver->vlans; i++) {
  228. sprintf(buf, "%d", i);
  229. priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
  230. if (driver->vlan_handlers != NULL)
  231. add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
  232. }
  233. priv->vlans[i] = NULL;
  234. return 0;
  235. }
  236. static inline int isspace(char c) {
  237. switch(c) {
  238. case ' ':
  239. case 0x09:
  240. case 0x0a:
  241. case 0x0d:
  242. return 1;
  243. default:
  244. return 0;
  245. }
  246. }
  247. #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
  248. #define islower(c) (((unsigned char)((c) - 'a')) < 26)
  249. int switch_parse_media(char *buf)
  250. {
  251. char *str = buf;
  252. while (*buf != 0) {
  253. *buf = toupper(*buf);
  254. buf++;
  255. }
  256. if (strncmp(str, "AUTO", 4) == 0)
  257. return SWITCH_MEDIA_AUTO;
  258. else if (strncmp(str, "100FD", 5) == 0)
  259. return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
  260. else if (strncmp(str, "100HD", 5) == 0)
  261. return SWITCH_MEDIA_100;
  262. else if (strncmp(str, "10FD", 4) == 0)
  263. return SWITCH_MEDIA_FD;
  264. else if (strncmp(str, "10HD", 4) == 0)
  265. return 0;
  266. else return -1;
  267. }
  268. int switch_print_media(char *buf, int media)
  269. {
  270. int len = 0;
  271. if (media & SWITCH_MEDIA_AUTO)
  272. len = sprintf(buf, "Auto");
  273. else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
  274. len = sprintf(buf, "100FD");
  275. else if (media == SWITCH_MEDIA_100)
  276. len = sprintf(buf, "100HD");
  277. else if (media == SWITCH_MEDIA_FD)
  278. len = sprintf(buf, "10FD");
  279. else if (media == 0)
  280. len = sprintf(buf, "10HD");
  281. else
  282. len = sprintf(buf, "Invalid");
  283. return len;
  284. }
  285. switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
  286. {
  287. switch_vlan_config *c;
  288. int j, u, p, s;
  289. c = kzalloc(sizeof(switch_vlan_config), GFP_KERNEL);
  290. if (!c)
  291. return NULL;
  292. while (isspace(*buf)) buf++;
  293. j = 0;
  294. while (*buf >= '0' && *buf <= '9') {
  295. j *= 10;
  296. j += *buf++ - '0';
  297. u = ((j == driver->cpuport) ? 0 : 1);
  298. p = 0;
  299. s = !(*buf >= '0' && *buf <= '9');
  300. if (s) {
  301. while (s && !isspace(*buf) && (*buf != 0)) {
  302. switch(*buf) {
  303. case 'u':
  304. u = 1;
  305. break;
  306. case 't':
  307. u = 0;
  308. break;
  309. case '*':
  310. p = 1;
  311. break;
  312. }
  313. buf++;
  314. }
  315. c->port |= (1 << j);
  316. if (u)
  317. c->untag |= (1 << j);
  318. if (p)
  319. c->pvid |= (1 << j);
  320. j = 0;
  321. }
  322. while (isspace(*buf)) buf++;
  323. }
  324. if (*buf != 0) {
  325. kfree(c);
  326. return NULL;
  327. }
  328. c->port &= (1 << driver->ports) - 1;
  329. c->untag &= (1 << driver->ports) - 1;
  330. c->pvid &= (1 << driver->ports) - 1;
  331. return c;
  332. }
  333. int switch_device_registered (char* device) {
  334. struct list_head *pos;
  335. list_for_each(pos, &drivers.list) {
  336. if (strcmp(list_entry(pos, switch_driver, list)->interface, device) == 0) {
  337. printk("There is already a switch registered on the device '%s'\n", device);
  338. return -EINVAL;
  339. }
  340. }
  341. return 0;
  342. }
  343. int switch_register_driver(switch_driver *driver)
  344. {
  345. struct list_head *pos;
  346. switch_driver *new;
  347. int ret;
  348. list_for_each(pos, &drivers.list) {
  349. if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
  350. printk("Switch driver '%s' already exists in the kernel\n", driver->name);
  351. return -EINVAL;
  352. }
  353. if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
  354. printk("There is already a switch registered on the device '%s'\n", driver->interface);
  355. return -EINVAL;
  356. }
  357. }
  358. new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
  359. if (!new)
  360. return -ENOMEM;
  361. memcpy(new, driver, sizeof(switch_driver));
  362. new->name = strdup(driver->name);
  363. new->interface = strdup(driver->interface);
  364. if ((ret = do_register(new)) < 0) {
  365. kfree(new->name);
  366. kfree(new);
  367. return ret;
  368. }
  369. INIT_LIST_HEAD(&new->list);
  370. list_add(&new->list, &drivers.list);
  371. return 0;
  372. }
  373. void switch_unregister_driver(char *name) {
  374. struct list_head *pos, *q;
  375. switch_driver *tmp;
  376. list_for_each_safe(pos, q, &drivers.list) {
  377. tmp = list_entry(pos, switch_driver, list);
  378. if (strcmp(tmp->name, name) == 0) {
  379. do_unregister(tmp);
  380. list_del(pos);
  381. kfree(tmp->name);
  382. kfree(tmp);
  383. return;
  384. }
  385. }
  386. }
  387. static int __init switch_init(void)
  388. {
  389. if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
  390. printk("%s: proc_mkdir failed.\n", __FILE__);
  391. return -ENODEV;
  392. }
  393. INIT_LIST_HEAD(&drivers.list);
  394. return 0;
  395. }
  396. static void __exit switch_exit(void)
  397. {
  398. remove_proc_entry("switch", NULL);
  399. }
  400. MODULE_AUTHOR("Felix Fietkau <[email protected]>");
  401. MODULE_LICENSE("GPL");
  402. EXPORT_SYMBOL(switch_device_registered);
  403. EXPORT_SYMBOL(switch_register_driver);
  404. EXPORT_SYMBOL(switch_unregister_driver);
  405. EXPORT_SYMBOL(switch_parse_vlan);
  406. EXPORT_SYMBOL(switch_parse_media);
  407. EXPORT_SYMBOL(switch_print_media);
  408. module_init(switch_init);
  409. module_exit(switch_exit);