common.c 29 KB

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