common.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/ctype.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers <Eilers.Stefan@epost.de>"
  21. #define DRIVER_DESC "Driver for Gigaset 307x"
  22. /* Module parameters */
  23. int gigaset_debuglevel = DEBUG_DEFAULT;
  24. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  25. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
  26. MODULE_PARM_DESC(debug, "debug level");
  27. /*======================================================================
  28. Prototypes of internal functions
  29. */
  30. static struct cardstate *alloc_cs(struct gigaset_driver *drv);
  31. static void free_cs(struct cardstate *cs);
  32. static void make_valid(struct cardstate *cs, unsigned mask);
  33. static void make_invalid(struct cardstate *cs, unsigned mask);
  34. #define VALID_MINOR 0x01
  35. #define VALID_ID 0x02
  36. #define ASSIGNED 0x04
  37. /* bitwise byte inversion table */
  38. __u8 gigaset_invtab[256] = {
  39. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  40. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  41. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  42. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  43. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  44. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  45. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  46. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  47. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  48. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  49. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  50. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  51. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  52. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  53. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  54. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  55. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  56. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  57. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  58. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  59. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  60. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  61. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  62. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  63. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  64. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  65. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  66. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  67. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  68. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  69. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  70. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  71. };
  72. EXPORT_SYMBOL_GPL(gigaset_invtab);
  73. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  74. size_t len, const unsigned char *buf, int from_user)
  75. {
  76. unsigned char outbuf[80];
  77. unsigned char inbuf[80 - 1];
  78. unsigned char c;
  79. size_t numin;
  80. const unsigned char *in;
  81. size_t space = sizeof outbuf - 1;
  82. unsigned char *out = outbuf;
  83. if (!from_user) {
  84. in = buf;
  85. numin = len;
  86. } else {
  87. numin = len < sizeof inbuf ? len : sizeof inbuf;
  88. in = inbuf;
  89. if (copy_from_user(inbuf, (const unsigned char __user *) buf,
  90. numin)) {
  91. gig_dbg(level, "%s (%u bytes) - copy_from_user failed",
  92. msg, (unsigned) len);
  93. return;
  94. }
  95. }
  96. while (numin-- > 0) {
  97. c = *buf++;
  98. if (c == '~' || c == '^' || c == '\\') {
  99. if (space-- <= 0)
  100. break;
  101. *out++ = '\\';
  102. }
  103. if (c & 0x80) {
  104. if (space-- <= 0)
  105. break;
  106. *out++ = '~';
  107. c ^= 0x80;
  108. }
  109. if (c < 0x20 || c == 0x7f) {
  110. if (space-- <= 0)
  111. break;
  112. *out++ = '^';
  113. c ^= 0x40;
  114. }
  115. if (space-- <= 0)
  116. break;
  117. *out++ = c;
  118. }
  119. *out = 0;
  120. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  121. }
  122. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  123. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  124. {
  125. int r;
  126. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  127. cs->control_state = flags;
  128. if (r < 0)
  129. return r;
  130. if (delay) {
  131. set_current_state(TASK_INTERRUPTIBLE);
  132. schedule_timeout(delay * HZ / 1000);
  133. }
  134. return 0;
  135. }
  136. int gigaset_enterconfigmode(struct cardstate *cs)
  137. {
  138. int i, r;
  139. if (!atomic_read(&cs->connected)) {
  140. err("not connected!");
  141. return -1;
  142. }
  143. cs->control_state = TIOCM_RTS; //FIXME
  144. r = setflags(cs, TIOCM_DTR, 200);
  145. if (r < 0)
  146. goto error;
  147. r = setflags(cs, 0, 200);
  148. if (r < 0)
  149. goto error;
  150. for (i = 0; i < 5; ++i) {
  151. r = setflags(cs, TIOCM_RTS, 100);
  152. if (r < 0)
  153. goto error;
  154. r = setflags(cs, 0, 100);
  155. if (r < 0)
  156. goto error;
  157. }
  158. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  159. if (r < 0)
  160. goto error;
  161. return 0;
  162. error:
  163. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  164. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  165. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  166. return -1; //r
  167. }
  168. static int test_timeout(struct at_state_t *at_state)
  169. {
  170. if (!at_state->timer_expires)
  171. return 0;
  172. if (--at_state->timer_expires) {
  173. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  174. at_state, at_state->timer_expires);
  175. return 0;
  176. }
  177. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  178. atomic_read(&at_state->timer_index), NULL)) {
  179. //FIXME what should we do?
  180. }
  181. return 1;
  182. }
  183. static void timer_tick(unsigned long data)
  184. {
  185. struct cardstate *cs = (struct cardstate *) data;
  186. unsigned long flags;
  187. unsigned channel;
  188. struct at_state_t *at_state;
  189. int timeout = 0;
  190. spin_lock_irqsave(&cs->lock, flags);
  191. for (channel = 0; channel < cs->channels; ++channel)
  192. if (test_timeout(&cs->bcs[channel].at_state))
  193. timeout = 1;
  194. if (test_timeout(&cs->at_state))
  195. timeout = 1;
  196. list_for_each_entry(at_state, &cs->temp_at_states, list)
  197. if (test_timeout(at_state))
  198. timeout = 1;
  199. if (atomic_read(&cs->running)) {
  200. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  201. if (timeout) {
  202. gig_dbg(DEBUG_CMD, "scheduling timeout");
  203. tasklet_schedule(&cs->event_tasklet);
  204. }
  205. }
  206. spin_unlock_irqrestore(&cs->lock, flags);
  207. }
  208. int gigaset_get_channel(struct bc_state *bcs)
  209. {
  210. unsigned long flags;
  211. spin_lock_irqsave(&bcs->cs->lock, flags);
  212. if (bcs->use_count) {
  213. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  214. bcs->channel);
  215. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  216. return 0;
  217. }
  218. ++bcs->use_count;
  219. bcs->busy = 1;
  220. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  221. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  222. return 1;
  223. }
  224. void gigaset_free_channel(struct bc_state *bcs)
  225. {
  226. unsigned long flags;
  227. spin_lock_irqsave(&bcs->cs->lock, flags);
  228. if (!bcs->busy) {
  229. gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
  230. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  231. return;
  232. }
  233. --bcs->use_count;
  234. bcs->busy = 0;
  235. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  236. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  237. }
  238. int gigaset_get_channels(struct cardstate *cs)
  239. {
  240. unsigned long flags;
  241. int i;
  242. spin_lock_irqsave(&cs->lock, flags);
  243. for (i = 0; i < cs->channels; ++i)
  244. if (cs->bcs[i].use_count) {
  245. spin_unlock_irqrestore(&cs->lock, flags);
  246. gig_dbg(DEBUG_ANY, "could not allocate all channels");
  247. return 0;
  248. }
  249. for (i = 0; i < cs->channels; ++i)
  250. ++cs->bcs[i].use_count;
  251. spin_unlock_irqrestore(&cs->lock, flags);
  252. gig_dbg(DEBUG_ANY, "allocated all channels");
  253. return 1;
  254. }
  255. void gigaset_free_channels(struct cardstate *cs)
  256. {
  257. unsigned long flags;
  258. int i;
  259. gig_dbg(DEBUG_ANY, "unblocking all channels");
  260. spin_lock_irqsave(&cs->lock, flags);
  261. for (i = 0; i < cs->channels; ++i)
  262. --cs->bcs[i].use_count;
  263. spin_unlock_irqrestore(&cs->lock, flags);
  264. }
  265. void gigaset_block_channels(struct cardstate *cs)
  266. {
  267. unsigned long flags;
  268. int i;
  269. gig_dbg(DEBUG_ANY, "blocking all channels");
  270. spin_lock_irqsave(&cs->lock, flags);
  271. for (i = 0; i < cs->channels; ++i)
  272. ++cs->bcs[i].use_count;
  273. spin_unlock_irqrestore(&cs->lock, flags);
  274. }
  275. static void clear_events(struct cardstate *cs)
  276. {
  277. struct event_t *ev;
  278. unsigned head, tail;
  279. /* no locking needed (no reader/writer allowed) */
  280. head = atomic_read(&cs->ev_head);
  281. tail = atomic_read(&cs->ev_tail);
  282. while (tail != head) {
  283. ev = cs->events + head;
  284. kfree(ev->ptr);
  285. head = (head + 1) % MAX_EVENTS;
  286. }
  287. atomic_set(&cs->ev_head, tail);
  288. }
  289. struct event_t *gigaset_add_event(struct cardstate *cs,
  290. struct at_state_t *at_state, int type,
  291. void *ptr, int parameter, void *arg)
  292. {
  293. unsigned long flags;
  294. unsigned next, tail;
  295. struct event_t *event = NULL;
  296. spin_lock_irqsave(&cs->ev_lock, flags);
  297. tail = atomic_read(&cs->ev_tail);
  298. next = (tail + 1) % MAX_EVENTS;
  299. if (unlikely(next == atomic_read(&cs->ev_head)))
  300. err("event queue full");
  301. else {
  302. event = cs->events + tail;
  303. event->type = type;
  304. event->at_state = at_state;
  305. event->cid = -1;
  306. event->ptr = ptr;
  307. event->arg = arg;
  308. event->parameter = parameter;
  309. atomic_set(&cs->ev_tail, next);
  310. }
  311. spin_unlock_irqrestore(&cs->ev_lock, flags);
  312. return event;
  313. }
  314. EXPORT_SYMBOL_GPL(gigaset_add_event);
  315. static void free_strings(struct at_state_t *at_state)
  316. {
  317. int i;
  318. for (i = 0; i < STR_NUM; ++i) {
  319. kfree(at_state->str_var[i]);
  320. at_state->str_var[i] = NULL;
  321. }
  322. }
  323. static void clear_at_state(struct at_state_t *at_state)
  324. {
  325. free_strings(at_state);
  326. }
  327. static void dealloc_at_states(struct cardstate *cs)
  328. {
  329. struct at_state_t *cur, *next;
  330. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  331. list_del(&cur->list);
  332. free_strings(cur);
  333. kfree(cur);
  334. }
  335. }
  336. static void gigaset_freebcs(struct bc_state *bcs)
  337. {
  338. int i;
  339. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  340. if (!bcs->cs->ops->freebcshw(bcs)) {
  341. gig_dbg(DEBUG_INIT, "failed");
  342. }
  343. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  344. clear_at_state(&bcs->at_state);
  345. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  346. if (bcs->skb)
  347. dev_kfree_skb(bcs->skb);
  348. for (i = 0; i < AT_NUM; ++i) {
  349. kfree(bcs->commands[i]);
  350. bcs->commands[i] = NULL;
  351. }
  352. }
  353. void gigaset_freecs(struct cardstate *cs)
  354. {
  355. int i;
  356. unsigned long flags;
  357. if (!cs)
  358. return;
  359. down(&cs->sem);
  360. if (!cs->bcs)
  361. goto f_cs;
  362. if (!cs->inbuf)
  363. goto f_bcs;
  364. spin_lock_irqsave(&cs->lock, flags);
  365. atomic_set(&cs->running, 0);
  366. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  367. not rescheduled below */
  368. tasklet_kill(&cs->event_tasklet);
  369. del_timer_sync(&cs->timer);
  370. switch (cs->cs_init) {
  371. default:
  372. gigaset_if_free(cs);
  373. gig_dbg(DEBUG_INIT, "clearing hw");
  374. cs->ops->freecshw(cs);
  375. //FIXME cmdbuf
  376. /* fall through */
  377. case 2: /* error in initcshw */
  378. /* Deregister from LL */
  379. make_invalid(cs, VALID_ID);
  380. gig_dbg(DEBUG_INIT, "clearing iif");
  381. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  382. /* fall through */
  383. case 1: /* error when regestering to LL */
  384. gig_dbg(DEBUG_INIT, "clearing at_state");
  385. clear_at_state(&cs->at_state);
  386. dealloc_at_states(cs);
  387. /* fall through */
  388. case 0: /* error in one call to initbcs */
  389. for (i = 0; i < cs->channels; ++i) {
  390. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  391. gigaset_freebcs(cs->bcs + i);
  392. }
  393. clear_events(cs);
  394. gig_dbg(DEBUG_INIT, "freeing inbuf");
  395. kfree(cs->inbuf);
  396. }
  397. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  398. kfree(cs->bcs);
  399. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  400. up(&cs->sem);
  401. free_cs(cs);
  402. }
  403. EXPORT_SYMBOL_GPL(gigaset_freecs);
  404. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  405. struct cardstate *cs, int cid)
  406. {
  407. int i;
  408. INIT_LIST_HEAD(&at_state->list);
  409. at_state->waiting = 0;
  410. at_state->getstring = 0;
  411. at_state->pending_commands = 0;
  412. at_state->timer_expires = 0;
  413. at_state->timer_active = 0;
  414. atomic_set(&at_state->timer_index, 0);
  415. atomic_set(&at_state->seq_index, 0);
  416. at_state->ConState = 0;
  417. for (i = 0; i < STR_NUM; ++i)
  418. at_state->str_var[i] = NULL;
  419. at_state->int_var[VAR_ZDLE] = 0;
  420. at_state->int_var[VAR_ZCTP] = -1;
  421. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  422. at_state->cs = cs;
  423. at_state->bcs = bcs;
  424. at_state->cid = cid;
  425. if (!cid)
  426. at_state->replystruct = cs->tabnocid;
  427. else
  428. at_state->replystruct = cs->tabcid;
  429. }
  430. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  431. struct cardstate *cs, int inputstate)
  432. /* inbuf->read must be allocated before! */
  433. {
  434. atomic_set(&inbuf->head, 0);
  435. atomic_set(&inbuf->tail, 0);
  436. inbuf->cs = cs;
  437. inbuf->bcs = bcs; /*base driver: NULL*/
  438. inbuf->rcvbuf = NULL; //FIXME
  439. inbuf->inputstate = inputstate;
  440. }
  441. /* Initialize the b-channel structure */
  442. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  443. struct cardstate *cs, int channel)
  444. {
  445. int i;
  446. bcs->tx_skb = NULL; //FIXME -> hw part
  447. skb_queue_head_init(&bcs->squeue);
  448. bcs->corrupted = 0;
  449. bcs->trans_down = 0;
  450. bcs->trans_up = 0;
  451. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  452. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  453. bcs->rcvbytes = 0;
  454. #ifdef CONFIG_GIGASET_DEBUG
  455. bcs->emptycount = 0;
  456. #endif
  457. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  458. bcs->fcs = PPP_INITFCS;
  459. bcs->inputstate = 0;
  460. if (cs->ignoreframes) {
  461. bcs->inputstate |= INS_skip_frame;
  462. bcs->skb = NULL;
  463. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  464. skb_reserve(bcs->skb, HW_HDR_LEN);
  465. else {
  466. dev_warn(cs->dev, "could not allocate skb\n");
  467. bcs->inputstate |= INS_skip_frame;
  468. }
  469. bcs->channel = channel;
  470. bcs->cs = cs;
  471. bcs->chstate = 0;
  472. bcs->use_count = 1;
  473. bcs->busy = 0;
  474. bcs->ignore = cs->ignoreframes;
  475. for (i = 0; i < AT_NUM; ++i)
  476. bcs->commands[i] = NULL;
  477. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  478. if (cs->ops->initbcshw(bcs))
  479. return bcs;
  480. gig_dbg(DEBUG_INIT, " failed");
  481. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  482. if (bcs->skb)
  483. dev_kfree_skb(bcs->skb);
  484. return NULL;
  485. }
  486. /* gigaset_initcs
  487. * Allocate and initialize cardstate structure for Gigaset driver
  488. * Calls hardware dependent gigaset_initcshw() function
  489. * Calls B channel initialization function gigaset_initbcs() for each B channel
  490. * parameters:
  491. * drv hardware driver the device belongs to
  492. * channels number of B channels supported by device
  493. * onechannel !=0: B channel data and AT commands share one
  494. * communication channel
  495. * ==0: B channels have separate communication channels
  496. * ignoreframes number of frames to ignore after setting up B channel
  497. * cidmode !=0: start in CallID mode
  498. * modulename name of driver module (used for I4L registration)
  499. * return value:
  500. * pointer to cardstate structure
  501. */
  502. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  503. int onechannel, int ignoreframes,
  504. int cidmode, const char *modulename)
  505. {
  506. struct cardstate *cs = NULL;
  507. int i;
  508. gig_dbg(DEBUG_INIT, "allocating cs");
  509. cs = alloc_cs(drv);
  510. if (!cs)
  511. goto error;
  512. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  513. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  514. if (!cs->bcs)
  515. goto error;
  516. gig_dbg(DEBUG_INIT, "allocating inbuf");
  517. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  518. if (!cs->inbuf)
  519. goto error;
  520. cs->cs_init = 0;
  521. cs->channels = channels;
  522. cs->onechannel = onechannel;
  523. cs->ignoreframes = ignoreframes;
  524. INIT_LIST_HEAD(&cs->temp_at_states);
  525. atomic_set(&cs->running, 0);
  526. init_timer(&cs->timer); /* clear next & prev */
  527. spin_lock_init(&cs->ev_lock);
  528. atomic_set(&cs->ev_tail, 0);
  529. atomic_set(&cs->ev_head, 0);
  530. init_MUTEX_LOCKED(&cs->sem);
  531. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  532. (unsigned long) cs);
  533. atomic_set(&cs->commands_pending, 0);
  534. cs->cur_at_seq = 0;
  535. cs->gotfwver = -1;
  536. cs->open_count = 0;
  537. cs->dev = NULL;
  538. cs->tty = NULL;
  539. atomic_set(&cs->cidmode, cidmode != 0);
  540. //if(onechannel) { //FIXME
  541. cs->tabnocid = gigaset_tab_nocid_m10x;
  542. cs->tabcid = gigaset_tab_cid_m10x;
  543. //} else {
  544. // cs->tabnocid = gigaset_tab_nocid;
  545. // cs->tabcid = gigaset_tab_cid;
  546. //}
  547. init_waitqueue_head(&cs->waitqueue);
  548. cs->waiting = 0;
  549. atomic_set(&cs->mode, M_UNKNOWN);
  550. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  551. for (i = 0; i < channels; ++i) {
  552. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  553. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  554. goto error;
  555. }
  556. ++cs->cs_init;
  557. gig_dbg(DEBUG_INIT, "setting up at_state");
  558. spin_lock_init(&cs->lock);
  559. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  560. cs->dle = 0;
  561. cs->cbytes = 0;
  562. gig_dbg(DEBUG_INIT, "setting up inbuf");
  563. if (onechannel) { //FIXME distinction necessary?
  564. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  565. } else
  566. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  567. atomic_set(&cs->connected, 0);
  568. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  569. cs->cmdbuf = cs->lastcmdbuf = NULL;
  570. spin_lock_init(&cs->cmdlock);
  571. cs->curlen = 0;
  572. cs->cmdbytes = 0;
  573. gig_dbg(DEBUG_INIT, "setting up iif");
  574. if (!gigaset_register_to_LL(cs, modulename)) {
  575. err("register_isdn failed");
  576. goto error;
  577. }
  578. make_valid(cs, VALID_ID);
  579. ++cs->cs_init;
  580. gig_dbg(DEBUG_INIT, "setting up hw");
  581. if (!cs->ops->initcshw(cs))
  582. goto error;
  583. ++cs->cs_init;
  584. gigaset_if_init(cs);
  585. atomic_set(&cs->running, 1);
  586. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  587. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  588. /* FIXME: can jiffies increase too much until the timer is added?
  589. * Same problem(?) with mod_timer() in timer_tick(). */
  590. add_timer(&cs->timer);
  591. gig_dbg(DEBUG_INIT, "cs initialized");
  592. up(&cs->sem);
  593. return cs;
  594. error: if (cs)
  595. up(&cs->sem);
  596. gig_dbg(DEBUG_INIT, "failed");
  597. gigaset_freecs(cs);
  598. return NULL;
  599. }
  600. EXPORT_SYMBOL_GPL(gigaset_initcs);
  601. /* ReInitialize the b-channel structure */
  602. /* e.g. called on hangup, disconnect */
  603. void gigaset_bcs_reinit(struct bc_state *bcs)
  604. {
  605. struct sk_buff *skb;
  606. struct cardstate *cs = bcs->cs;
  607. unsigned long flags;
  608. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  609. dev_kfree_skb(skb);
  610. spin_lock_irqsave(&cs->lock, flags);
  611. clear_at_state(&bcs->at_state);
  612. bcs->at_state.ConState = 0;
  613. bcs->at_state.timer_active = 0;
  614. bcs->at_state.timer_expires = 0;
  615. bcs->at_state.cid = -1; /* No CID defined */
  616. spin_unlock_irqrestore(&cs->lock, flags);
  617. bcs->inputstate = 0;
  618. #ifdef CONFIG_GIGASET_DEBUG
  619. bcs->emptycount = 0;
  620. #endif
  621. bcs->fcs = PPP_INITFCS;
  622. bcs->chstate = 0;
  623. bcs->ignore = cs->ignoreframes;
  624. if (bcs->ignore)
  625. bcs->inputstate |= INS_skip_frame;
  626. cs->ops->reinitbcshw(bcs);
  627. }
  628. static void cleanup_cs(struct cardstate *cs)
  629. {
  630. struct cmdbuf_t *cb, *tcb;
  631. int i;
  632. unsigned long flags;
  633. spin_lock_irqsave(&cs->lock, flags);
  634. atomic_set(&cs->mode, M_UNKNOWN);
  635. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  636. clear_at_state(&cs->at_state);
  637. dealloc_at_states(cs);
  638. free_strings(&cs->at_state);
  639. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  640. kfree(cs->inbuf->rcvbuf);
  641. cs->inbuf->rcvbuf = NULL;
  642. cs->inbuf->inputstate = INS_command;
  643. atomic_set(&cs->inbuf->head, 0);
  644. atomic_set(&cs->inbuf->tail, 0);
  645. cb = cs->cmdbuf;
  646. while (cb) {
  647. tcb = cb;
  648. cb = cb->next;
  649. kfree(tcb);
  650. }
  651. cs->cmdbuf = cs->lastcmdbuf = NULL;
  652. cs->curlen = 0;
  653. cs->cmdbytes = 0;
  654. cs->gotfwver = -1;
  655. cs->dle = 0;
  656. cs->cur_at_seq = 0;
  657. atomic_set(&cs->commands_pending, 0);
  658. cs->cbytes = 0;
  659. spin_unlock_irqrestore(&cs->lock, flags);
  660. for (i = 0; i < cs->channels; ++i) {
  661. gigaset_freebcs(cs->bcs + i);
  662. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  663. break; //FIXME error handling
  664. }
  665. if (cs->waiting) {
  666. cs->cmd_result = -ENODEV;
  667. cs->waiting = 0;
  668. wake_up_interruptible(&cs->waitqueue);
  669. }
  670. }
  671. int gigaset_start(struct cardstate *cs)
  672. {
  673. if (down_interruptible(&cs->sem))
  674. return 0;
  675. atomic_set(&cs->connected, 1);
  676. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  677. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  678. cs->ops->baud_rate(cs, B115200);
  679. cs->ops->set_line_ctrl(cs, CS8);
  680. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  681. } else {
  682. //FIXME use some saved values?
  683. }
  684. cs->waiting = 1;
  685. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  686. cs->waiting = 0;
  687. //FIXME what should we do?
  688. goto error;
  689. }
  690. gig_dbg(DEBUG_CMD, "scheduling START");
  691. gigaset_schedule_event(cs);
  692. wait_event(cs->waitqueue, !cs->waiting);
  693. up(&cs->sem);
  694. return 1;
  695. error:
  696. up(&cs->sem);
  697. return 0;
  698. }
  699. EXPORT_SYMBOL_GPL(gigaset_start);
  700. void gigaset_shutdown(struct cardstate *cs)
  701. {
  702. down(&cs->sem);
  703. cs->waiting = 1;
  704. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  705. //FIXME what should we do?
  706. goto exit;
  707. }
  708. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  709. gigaset_schedule_event(cs);
  710. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  711. warn("%s: aborted", __func__);
  712. //FIXME
  713. }
  714. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  715. //FIXME?
  716. //gigaset_baud_rate(cs, B115200);
  717. //gigaset_set_line_ctrl(cs, CS8);
  718. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  719. //cs->control_state = 0;
  720. } else {
  721. //FIXME use some saved values?
  722. }
  723. cleanup_cs(cs);
  724. exit:
  725. up(&cs->sem);
  726. }
  727. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  728. void gigaset_stop(struct cardstate *cs)
  729. {
  730. down(&cs->sem);
  731. atomic_set(&cs->connected, 0);
  732. cs->waiting = 1;
  733. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  734. //FIXME what should we do?
  735. goto exit;
  736. }
  737. gig_dbg(DEBUG_CMD, "scheduling STOP");
  738. gigaset_schedule_event(cs);
  739. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  740. warn("%s: aborted", __func__);
  741. //FIXME
  742. }
  743. /* Tell the LL that the device is not available .. */
  744. gigaset_i4l_cmd(cs, ISDN_STAT_STOP); // FIXME move to event layer?
  745. cleanup_cs(cs);
  746. exit:
  747. up(&cs->sem);
  748. }
  749. EXPORT_SYMBOL_GPL(gigaset_stop);
  750. static LIST_HEAD(drivers);
  751. static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
  752. struct cardstate *gigaset_get_cs_by_id(int id)
  753. {
  754. unsigned long flags;
  755. static struct cardstate *ret = NULL;
  756. static struct cardstate *cs;
  757. struct gigaset_driver *drv;
  758. unsigned i;
  759. spin_lock_irqsave(&driver_lock, flags);
  760. list_for_each_entry(drv, &drivers, list) {
  761. spin_lock(&drv->lock);
  762. for (i = 0; i < drv->minors; ++i) {
  763. if (drv->flags[i] & VALID_ID) {
  764. cs = drv->cs + i;
  765. if (cs->myid == id)
  766. ret = cs;
  767. }
  768. if (ret)
  769. break;
  770. }
  771. spin_unlock(&drv->lock);
  772. if (ret)
  773. break;
  774. }
  775. spin_unlock_irqrestore(&driver_lock, flags);
  776. return ret;
  777. }
  778. void gigaset_debugdrivers(void)
  779. {
  780. unsigned long flags;
  781. static struct cardstate *cs;
  782. struct gigaset_driver *drv;
  783. unsigned i;
  784. spin_lock_irqsave(&driver_lock, flags);
  785. list_for_each_entry(drv, &drivers, list) {
  786. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  787. spin_lock(&drv->lock);
  788. for (i = 0; i < drv->minors; ++i) {
  789. gig_dbg(DEBUG_DRIVER, " index %u", i);
  790. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  791. drv->flags[i]);
  792. cs = drv->cs + i;
  793. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  794. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  795. cs->minor_index);
  796. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  797. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  798. }
  799. spin_unlock(&drv->lock);
  800. }
  801. spin_unlock_irqrestore(&driver_lock, flags);
  802. }
  803. EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
  804. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  805. {
  806. if (tty->index < 0 || tty->index >= tty->driver->num)
  807. return NULL;
  808. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  809. }
  810. struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  811. {
  812. unsigned long flags;
  813. static struct cardstate *ret = NULL;
  814. struct gigaset_driver *drv;
  815. unsigned index;
  816. spin_lock_irqsave(&driver_lock, flags);
  817. list_for_each_entry(drv, &drivers, list) {
  818. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  819. continue;
  820. index = minor - drv->minor;
  821. spin_lock(&drv->lock);
  822. if (drv->flags[index] & VALID_MINOR)
  823. ret = drv->cs + index;
  824. spin_unlock(&drv->lock);
  825. if (ret)
  826. break;
  827. }
  828. spin_unlock_irqrestore(&driver_lock, flags);
  829. return ret;
  830. }
  831. void gigaset_freedriver(struct gigaset_driver *drv)
  832. {
  833. unsigned long flags;
  834. spin_lock_irqsave(&driver_lock, flags);
  835. list_del(&drv->list);
  836. spin_unlock_irqrestore(&driver_lock, flags);
  837. gigaset_if_freedriver(drv);
  838. module_put(drv->owner);
  839. kfree(drv->cs);
  840. kfree(drv->flags);
  841. kfree(drv);
  842. }
  843. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  844. /* gigaset_initdriver
  845. * Allocate and initialize gigaset_driver structure. Initialize interface.
  846. * parameters:
  847. * minor First minor number
  848. * minors Number of minors this driver can handle
  849. * procname Name of the driver
  850. * devname Name of the device files (prefix without minor number)
  851. * devfsname Devfs name of the device files without %d
  852. * return value:
  853. * Pointer to the gigaset_driver structure on success, NULL on failure.
  854. */
  855. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  856. const char *procname,
  857. const char *devname,
  858. const char *devfsname,
  859. const struct gigaset_ops *ops,
  860. struct module *owner)
  861. {
  862. struct gigaset_driver *drv;
  863. unsigned long flags;
  864. unsigned i;
  865. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  866. if (!drv)
  867. return NULL;
  868. if (!try_module_get(owner))
  869. return NULL;
  870. drv->cs = NULL;
  871. drv->have_tty = 0;
  872. drv->minor = minor;
  873. drv->minors = minors;
  874. spin_lock_init(&drv->lock);
  875. drv->blocked = 0;
  876. drv->ops = ops;
  877. drv->owner = owner;
  878. INIT_LIST_HEAD(&drv->list);
  879. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  880. if (!drv->cs)
  881. goto out1;
  882. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  883. if (!drv->flags)
  884. goto out2;
  885. for (i = 0; i < minors; ++i) {
  886. drv->flags[i] = 0;
  887. drv->cs[i].driver = drv;
  888. drv->cs[i].ops = drv->ops;
  889. drv->cs[i].minor_index = i;
  890. }
  891. gigaset_if_initdriver(drv, procname, devname, devfsname);
  892. spin_lock_irqsave(&driver_lock, flags);
  893. list_add(&drv->list, &drivers);
  894. spin_unlock_irqrestore(&driver_lock, flags);
  895. return drv;
  896. out2:
  897. kfree(drv->cs);
  898. out1:
  899. kfree(drv);
  900. module_put(owner);
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  904. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  905. {
  906. unsigned long flags;
  907. unsigned i;
  908. static struct cardstate *ret = NULL;
  909. spin_lock_irqsave(&drv->lock, flags);
  910. for (i = 0; i < drv->minors; ++i) {
  911. if (!(drv->flags[i] & VALID_MINOR)) {
  912. drv->flags[i] = VALID_MINOR;
  913. ret = drv->cs + i;
  914. }
  915. if (ret)
  916. break;
  917. }
  918. spin_unlock_irqrestore(&drv->lock, flags);
  919. return ret;
  920. }
  921. static void free_cs(struct cardstate *cs)
  922. {
  923. unsigned long flags;
  924. struct gigaset_driver *drv = cs->driver;
  925. spin_lock_irqsave(&drv->lock, flags);
  926. drv->flags[cs->minor_index] = 0;
  927. spin_unlock_irqrestore(&drv->lock, flags);
  928. }
  929. static void make_valid(struct cardstate *cs, unsigned mask)
  930. {
  931. unsigned long flags;
  932. struct gigaset_driver *drv = cs->driver;
  933. spin_lock_irqsave(&drv->lock, flags);
  934. drv->flags[cs->minor_index] |= mask;
  935. spin_unlock_irqrestore(&drv->lock, flags);
  936. }
  937. static void make_invalid(struct cardstate *cs, unsigned mask)
  938. {
  939. unsigned long flags;
  940. struct gigaset_driver *drv = cs->driver;
  941. spin_lock_irqsave(&drv->lock, flags);
  942. drv->flags[cs->minor_index] &= ~mask;
  943. spin_unlock_irqrestore(&drv->lock, flags);
  944. }
  945. /* For drivers without fixed assignment device<->cardstate (usb) */
  946. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  947. {
  948. unsigned long flags;
  949. struct cardstate *cs = NULL;
  950. unsigned i;
  951. spin_lock_irqsave(&drv->lock, flags);
  952. if (drv->blocked)
  953. goto exit;
  954. for (i = 0; i < drv->minors; ++i) {
  955. if ((drv->flags[i] & VALID_MINOR) &&
  956. !(drv->flags[i] & ASSIGNED)) {
  957. drv->flags[i] |= ASSIGNED;
  958. cs = drv->cs + i;
  959. break;
  960. }
  961. }
  962. exit:
  963. spin_unlock_irqrestore(&drv->lock, flags);
  964. return cs;
  965. }
  966. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  967. void gigaset_unassign(struct cardstate *cs)
  968. {
  969. unsigned long flags;
  970. unsigned *minor_flags;
  971. struct gigaset_driver *drv;
  972. if (!cs)
  973. return;
  974. drv = cs->driver;
  975. spin_lock_irqsave(&drv->lock, flags);
  976. minor_flags = drv->flags + cs->minor_index;
  977. if (*minor_flags & VALID_MINOR)
  978. *minor_flags &= ~ASSIGNED;
  979. spin_unlock_irqrestore(&drv->lock, flags);
  980. }
  981. EXPORT_SYMBOL_GPL(gigaset_unassign);
  982. void gigaset_blockdriver(struct gigaset_driver *drv)
  983. {
  984. unsigned long flags;
  985. spin_lock_irqsave(&drv->lock, flags);
  986. drv->blocked = 1;
  987. spin_unlock_irqrestore(&drv->lock, flags);
  988. }
  989. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  990. static int __init gigaset_init_module(void)
  991. {
  992. /* in accordance with the principle of least astonishment,
  993. * setting the 'debug' parameter to 1 activates a sensible
  994. * set of default debug levels
  995. */
  996. if (gigaset_debuglevel == 1)
  997. gigaset_debuglevel = DEBUG_DEFAULT;
  998. info(DRIVER_AUTHOR);
  999. info(DRIVER_DESC);
  1000. return 0;
  1001. }
  1002. static void __exit gigaset_exit_module(void)
  1003. {
  1004. }
  1005. module_init(gigaset_init_module);
  1006. module_exit(gigaset_exit_module);
  1007. MODULE_AUTHOR(DRIVER_AUTHOR);
  1008. MODULE_DESCRIPTION(DRIVER_DESC);
  1009. MODULE_LICENSE("GPL");