common.c 29 KB

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