common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers,
  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/module.h>
  17. #include <linux/moduleparam.h>
  18. /* Version Information */
  19. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  20. #define DRIVER_DESC "Driver for Gigaset 307x"
  21. #ifdef CONFIG_GIGASET_DEBUG
  22. #define DRIVER_DESC_DEBUG " (debug build)"
  23. #else
  24. #define DRIVER_DESC_DEBUG ""
  25. #endif
  26. /* Module parameters */
  27. int gigaset_debuglevel;
  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. /* driver state flags */
  32. #define VALID_MINOR 0x01
  33. #define VALID_ID 0x02
  34. /**
  35. * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
  36. * @level: debugging level.
  37. * @msg: message prefix.
  38. * @len: number of bytes to dump.
  39. * @buf: data to dump.
  40. *
  41. * If the current debugging level includes one of the bits set in @level,
  42. * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
  43. * prefixed by the text @msg.
  44. */
  45. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  46. size_t len, const unsigned char *buf)
  47. {
  48. unsigned char outbuf[80];
  49. unsigned char c;
  50. size_t space = sizeof outbuf - 1;
  51. unsigned char *out = outbuf;
  52. size_t numin = len;
  53. while (numin--) {
  54. c = *buf++;
  55. if (c == '~' || c == '^' || c == '\\') {
  56. if (!space--)
  57. break;
  58. *out++ = '\\';
  59. }
  60. if (c & 0x80) {
  61. if (!space--)
  62. break;
  63. *out++ = '~';
  64. c ^= 0x80;
  65. }
  66. if (c < 0x20 || c == 0x7f) {
  67. if (!space--)
  68. break;
  69. *out++ = '^';
  70. c ^= 0x40;
  71. }
  72. if (!space--)
  73. break;
  74. *out++ = c;
  75. }
  76. *out = 0;
  77. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  78. }
  79. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  80. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  81. {
  82. int r;
  83. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  84. cs->control_state = flags;
  85. if (r < 0)
  86. return r;
  87. if (delay) {
  88. set_current_state(TASK_INTERRUPTIBLE);
  89. schedule_timeout(delay * HZ / 1000);
  90. }
  91. return 0;
  92. }
  93. int gigaset_enterconfigmode(struct cardstate *cs)
  94. {
  95. int i, r;
  96. cs->control_state = TIOCM_RTS;
  97. r = setflags(cs, TIOCM_DTR, 200);
  98. if (r < 0)
  99. goto error;
  100. r = setflags(cs, 0, 200);
  101. if (r < 0)
  102. goto error;
  103. for (i = 0; i < 5; ++i) {
  104. r = setflags(cs, TIOCM_RTS, 100);
  105. if (r < 0)
  106. goto error;
  107. r = setflags(cs, 0, 100);
  108. if (r < 0)
  109. goto error;
  110. }
  111. r = setflags(cs, TIOCM_RTS | TIOCM_DTR, 800);
  112. if (r < 0)
  113. goto error;
  114. return 0;
  115. error:
  116. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  117. cs->control_state = TIOCM_RTS | TIOCM_DTR;
  118. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS | TIOCM_DTR);
  119. return -1;
  120. }
  121. static int test_timeout(struct at_state_t *at_state)
  122. {
  123. if (!at_state->timer_expires)
  124. return 0;
  125. if (--at_state->timer_expires) {
  126. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  127. at_state, at_state->timer_expires);
  128. return 0;
  129. }
  130. gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  131. at_state->timer_index, NULL);
  132. return 1;
  133. }
  134. static void timer_tick(unsigned long data)
  135. {
  136. struct cardstate *cs = (struct cardstate *) data;
  137. unsigned long flags;
  138. unsigned channel;
  139. struct at_state_t *at_state;
  140. int timeout = 0;
  141. spin_lock_irqsave(&cs->lock, flags);
  142. for (channel = 0; channel < cs->channels; ++channel)
  143. if (test_timeout(&cs->bcs[channel].at_state))
  144. timeout = 1;
  145. if (test_timeout(&cs->at_state))
  146. timeout = 1;
  147. list_for_each_entry(at_state, &cs->temp_at_states, list)
  148. if (test_timeout(at_state))
  149. timeout = 1;
  150. if (cs->running) {
  151. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  152. if (timeout) {
  153. gig_dbg(DEBUG_EVENT, "scheduling timeout");
  154. tasklet_schedule(&cs->event_tasklet);
  155. }
  156. }
  157. spin_unlock_irqrestore(&cs->lock, flags);
  158. }
  159. int gigaset_get_channel(struct bc_state *bcs)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&bcs->cs->lock, flags);
  163. if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
  164. gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
  165. bcs->channel);
  166. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  167. return 0;
  168. }
  169. ++bcs->use_count;
  170. bcs->busy = 1;
  171. gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
  172. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  173. return 1;
  174. }
  175. struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
  176. {
  177. unsigned long flags;
  178. int i;
  179. spin_lock_irqsave(&cs->lock, flags);
  180. if (!try_module_get(cs->driver->owner)) {
  181. gig_dbg(DEBUG_CHANNEL,
  182. "could not get module for allocating channel");
  183. spin_unlock_irqrestore(&cs->lock, flags);
  184. return NULL;
  185. }
  186. for (i = 0; i < cs->channels; ++i)
  187. if (!cs->bcs[i].use_count) {
  188. ++cs->bcs[i].use_count;
  189. cs->bcs[i].busy = 1;
  190. spin_unlock_irqrestore(&cs->lock, flags);
  191. gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
  192. return cs->bcs + i;
  193. }
  194. module_put(cs->driver->owner);
  195. spin_unlock_irqrestore(&cs->lock, flags);
  196. gig_dbg(DEBUG_CHANNEL, "no free channel");
  197. return NULL;
  198. }
  199. void gigaset_free_channel(struct bc_state *bcs)
  200. {
  201. unsigned long flags;
  202. spin_lock_irqsave(&bcs->cs->lock, flags);
  203. if (!bcs->busy) {
  204. gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
  205. bcs->channel);
  206. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  207. return;
  208. }
  209. --bcs->use_count;
  210. bcs->busy = 0;
  211. module_put(bcs->cs->driver->owner);
  212. gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
  213. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  214. }
  215. int gigaset_get_channels(struct cardstate *cs)
  216. {
  217. unsigned long flags;
  218. int i;
  219. spin_lock_irqsave(&cs->lock, flags);
  220. for (i = 0; i < cs->channels; ++i)
  221. if (cs->bcs[i].use_count) {
  222. spin_unlock_irqrestore(&cs->lock, flags);
  223. gig_dbg(DEBUG_CHANNEL,
  224. "could not allocate all channels");
  225. return 0;
  226. }
  227. for (i = 0; i < cs->channels; ++i)
  228. ++cs->bcs[i].use_count;
  229. spin_unlock_irqrestore(&cs->lock, flags);
  230. gig_dbg(DEBUG_CHANNEL, "allocated all channels");
  231. return 1;
  232. }
  233. void gigaset_free_channels(struct cardstate *cs)
  234. {
  235. unsigned long flags;
  236. int i;
  237. gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
  238. spin_lock_irqsave(&cs->lock, flags);
  239. for (i = 0; i < cs->channels; ++i)
  240. --cs->bcs[i].use_count;
  241. spin_unlock_irqrestore(&cs->lock, flags);
  242. }
  243. void gigaset_block_channels(struct cardstate *cs)
  244. {
  245. unsigned long flags;
  246. int i;
  247. gig_dbg(DEBUG_CHANNEL, "blocking all channels");
  248. spin_lock_irqsave(&cs->lock, flags);
  249. for (i = 0; i < cs->channels; ++i)
  250. ++cs->bcs[i].use_count;
  251. spin_unlock_irqrestore(&cs->lock, flags);
  252. }
  253. static void clear_events(struct cardstate *cs)
  254. {
  255. struct event_t *ev;
  256. unsigned head, tail;
  257. unsigned long flags;
  258. spin_lock_irqsave(&cs->ev_lock, flags);
  259. head = cs->ev_head;
  260. tail = cs->ev_tail;
  261. while (tail != head) {
  262. ev = cs->events + head;
  263. kfree(ev->ptr);
  264. head = (head + 1) % MAX_EVENTS;
  265. }
  266. cs->ev_head = tail;
  267. spin_unlock_irqrestore(&cs->ev_lock, flags);
  268. }
  269. /**
  270. * gigaset_add_event() - add event to device event queue
  271. * @cs: device descriptor structure.
  272. * @at_state: connection state structure.
  273. * @type: event type.
  274. * @ptr: pointer parameter for event.
  275. * @parameter: integer parameter for event.
  276. * @arg: pointer parameter for event.
  277. *
  278. * Allocate an event queue entry from the device's event queue, and set it up
  279. * with the parameters given.
  280. *
  281. * Return value: added event
  282. */
  283. struct event_t *gigaset_add_event(struct cardstate *cs,
  284. struct at_state_t *at_state, int type,
  285. void *ptr, int parameter, void *arg)
  286. {
  287. unsigned long flags;
  288. unsigned next, tail;
  289. struct event_t *event = NULL;
  290. gig_dbg(DEBUG_EVENT, "queueing event %d", type);
  291. spin_lock_irqsave(&cs->ev_lock, flags);
  292. tail = cs->ev_tail;
  293. next = (tail + 1) % MAX_EVENTS;
  294. if (unlikely(next == cs->ev_head))
  295. dev_err(cs->dev, "event queue full\n");
  296. else {
  297. event = cs->events + tail;
  298. event->type = type;
  299. event->at_state = at_state;
  300. event->cid = -1;
  301. event->ptr = ptr;
  302. event->arg = arg;
  303. event->parameter = parameter;
  304. cs->ev_tail = next;
  305. }
  306. spin_unlock_irqrestore(&cs->ev_lock, flags);
  307. return event;
  308. }
  309. EXPORT_SYMBOL_GPL(gigaset_add_event);
  310. static void free_strings(struct at_state_t *at_state)
  311. {
  312. int i;
  313. for (i = 0; i < STR_NUM; ++i) {
  314. kfree(at_state->str_var[i]);
  315. at_state->str_var[i] = NULL;
  316. }
  317. }
  318. static void clear_at_state(struct at_state_t *at_state)
  319. {
  320. free_strings(at_state);
  321. }
  322. static void dealloc_at_states(struct cardstate *cs)
  323. {
  324. struct at_state_t *cur, *next;
  325. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  326. list_del(&cur->list);
  327. free_strings(cur);
  328. kfree(cur);
  329. }
  330. }
  331. static void gigaset_freebcs(struct bc_state *bcs)
  332. {
  333. int i;
  334. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  335. if (!bcs->cs->ops->freebcshw(bcs))
  336. gig_dbg(DEBUG_INIT, "failed");
  337. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  338. clear_at_state(&bcs->at_state);
  339. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  340. dev_kfree_skb(bcs->rx_skb);
  341. bcs->rx_skb = NULL;
  342. for (i = 0; i < AT_NUM; ++i) {
  343. kfree(bcs->commands[i]);
  344. bcs->commands[i] = NULL;
  345. }
  346. }
  347. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  348. {
  349. unsigned long flags;
  350. unsigned i;
  351. struct cardstate *cs;
  352. struct cardstate *ret = NULL;
  353. spin_lock_irqsave(&drv->lock, flags);
  354. if (drv->blocked)
  355. goto exit;
  356. for (i = 0; i < drv->minors; ++i) {
  357. cs = drv->cs + i;
  358. if (!(cs->flags & VALID_MINOR)) {
  359. cs->flags = VALID_MINOR;
  360. ret = cs;
  361. break;
  362. }
  363. }
  364. exit:
  365. spin_unlock_irqrestore(&drv->lock, flags);
  366. return ret;
  367. }
  368. static void free_cs(struct cardstate *cs)
  369. {
  370. cs->flags = 0;
  371. }
  372. static void make_valid(struct cardstate *cs, unsigned mask)
  373. {
  374. unsigned long flags;
  375. struct gigaset_driver *drv = cs->driver;
  376. spin_lock_irqsave(&drv->lock, flags);
  377. cs->flags |= mask;
  378. spin_unlock_irqrestore(&drv->lock, flags);
  379. }
  380. static void make_invalid(struct cardstate *cs, unsigned mask)
  381. {
  382. unsigned long flags;
  383. struct gigaset_driver *drv = cs->driver;
  384. spin_lock_irqsave(&drv->lock, flags);
  385. cs->flags &= ~mask;
  386. spin_unlock_irqrestore(&drv->lock, flags);
  387. }
  388. /**
  389. * gigaset_freecs() - free all associated ressources of a device
  390. * @cs: device descriptor structure.
  391. *
  392. * Stops all tasklets and timers, unregisters the device from all
  393. * subsystems it was registered to, deallocates the device structure
  394. * @cs and all structures referenced from it.
  395. * Operations on the device should be stopped before calling this.
  396. */
  397. void gigaset_freecs(struct cardstate *cs)
  398. {
  399. int i;
  400. unsigned long flags;
  401. if (!cs)
  402. return;
  403. mutex_lock(&cs->mutex);
  404. if (!cs->bcs)
  405. goto f_cs;
  406. if (!cs->inbuf)
  407. goto f_bcs;
  408. spin_lock_irqsave(&cs->lock, flags);
  409. cs->running = 0;
  410. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  411. not rescheduled below */
  412. tasklet_kill(&cs->event_tasklet);
  413. del_timer_sync(&cs->timer);
  414. switch (cs->cs_init) {
  415. default:
  416. /* clear B channel structures */
  417. for (i = 0; i < cs->channels; ++i) {
  418. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  419. gigaset_freebcs(cs->bcs + i);
  420. }
  421. /* clear device sysfs */
  422. gigaset_free_dev_sysfs(cs);
  423. gigaset_if_free(cs);
  424. gig_dbg(DEBUG_INIT, "clearing hw");
  425. cs->ops->freecshw(cs);
  426. /* fall through */
  427. case 2: /* error in initcshw */
  428. /* Deregister from LL */
  429. make_invalid(cs, VALID_ID);
  430. gigaset_isdn_unregdev(cs);
  431. /* fall through */
  432. case 1: /* error when registering to LL */
  433. gig_dbg(DEBUG_INIT, "clearing at_state");
  434. clear_at_state(&cs->at_state);
  435. dealloc_at_states(cs);
  436. /* fall through */
  437. case 0: /* error in basic setup */
  438. clear_events(cs);
  439. gig_dbg(DEBUG_INIT, "freeing inbuf");
  440. kfree(cs->inbuf);
  441. }
  442. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  443. kfree(cs->bcs);
  444. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  445. mutex_unlock(&cs->mutex);
  446. free_cs(cs);
  447. }
  448. EXPORT_SYMBOL_GPL(gigaset_freecs);
  449. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  450. struct cardstate *cs, int cid)
  451. {
  452. int i;
  453. INIT_LIST_HEAD(&at_state->list);
  454. at_state->waiting = 0;
  455. at_state->getstring = 0;
  456. at_state->pending_commands = 0;
  457. at_state->timer_expires = 0;
  458. at_state->timer_active = 0;
  459. at_state->timer_index = 0;
  460. at_state->seq_index = 0;
  461. at_state->ConState = 0;
  462. for (i = 0; i < STR_NUM; ++i)
  463. at_state->str_var[i] = NULL;
  464. at_state->int_var[VAR_ZDLE] = 0;
  465. at_state->int_var[VAR_ZCTP] = -1;
  466. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  467. at_state->cs = cs;
  468. at_state->bcs = bcs;
  469. at_state->cid = cid;
  470. if (!cid)
  471. at_state->replystruct = cs->tabnocid;
  472. else
  473. at_state->replystruct = cs->tabcid;
  474. }
  475. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
  476. /* inbuf->read must be allocated before! */
  477. {
  478. inbuf->head = 0;
  479. inbuf->tail = 0;
  480. inbuf->cs = cs;
  481. inbuf->inputstate = INS_command;
  482. }
  483. /**
  484. * gigaset_fill_inbuf() - append received data to input buffer
  485. * @inbuf: buffer structure.
  486. * @src: received data.
  487. * @numbytes: number of bytes received.
  488. */
  489. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  490. unsigned numbytes)
  491. {
  492. unsigned n, head, tail, bytesleft;
  493. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  494. if (!numbytes)
  495. return 0;
  496. bytesleft = numbytes;
  497. tail = inbuf->tail;
  498. head = inbuf->head;
  499. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  500. while (bytesleft) {
  501. if (head > tail)
  502. n = head - 1 - tail;
  503. else if (head == 0)
  504. n = (RBUFSIZE - 1) - tail;
  505. else
  506. n = RBUFSIZE - tail;
  507. if (!n) {
  508. dev_err(inbuf->cs->dev,
  509. "buffer overflow (%u bytes lost)\n",
  510. bytesleft);
  511. break;
  512. }
  513. if (n > bytesleft)
  514. n = bytesleft;
  515. memcpy(inbuf->data + tail, src, n);
  516. bytesleft -= n;
  517. tail = (tail + n) % RBUFSIZE;
  518. src += n;
  519. }
  520. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  521. inbuf->tail = tail;
  522. return numbytes != bytesleft;
  523. }
  524. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  525. /* Initialize the b-channel structure */
  526. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  527. struct cardstate *cs, int channel)
  528. {
  529. int i;
  530. bcs->tx_skb = NULL;
  531. skb_queue_head_init(&bcs->squeue);
  532. bcs->corrupted = 0;
  533. bcs->trans_down = 0;
  534. bcs->trans_up = 0;
  535. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  536. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  537. #ifdef CONFIG_GIGASET_DEBUG
  538. bcs->emptycount = 0;
  539. #endif
  540. bcs->rx_bufsize = 0;
  541. bcs->rx_skb = NULL;
  542. bcs->rx_fcs = PPP_INITFCS;
  543. bcs->inputstate = 0;
  544. bcs->channel = channel;
  545. bcs->cs = cs;
  546. bcs->chstate = 0;
  547. bcs->use_count = 1;
  548. bcs->busy = 0;
  549. bcs->ignore = cs->ignoreframes;
  550. for (i = 0; i < AT_NUM; ++i)
  551. bcs->commands[i] = NULL;
  552. spin_lock_init(&bcs->aplock);
  553. bcs->ap = NULL;
  554. bcs->apconnstate = 0;
  555. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  556. if (cs->ops->initbcshw(bcs))
  557. return bcs;
  558. gig_dbg(DEBUG_INIT, " failed");
  559. return NULL;
  560. }
  561. /**
  562. * gigaset_initcs() - initialize device structure
  563. * @drv: hardware driver the device belongs to
  564. * @channels: number of B channels supported by device
  565. * @onechannel: !=0 if B channel data and AT commands share one
  566. * communication channel (M10x),
  567. * ==0 if B channels have separate communication channels (base)
  568. * @ignoreframes: number of frames to ignore after setting up B channel
  569. * @cidmode: !=0: start in CallID mode
  570. * @modulename: name of driver module for LL registration
  571. *
  572. * Allocate and initialize cardstate structure for Gigaset driver
  573. * Calls hardware dependent gigaset_initcshw() function
  574. * Calls B channel initialization function gigaset_initbcs() for each B channel
  575. *
  576. * Return value:
  577. * pointer to cardstate structure
  578. */
  579. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  580. int onechannel, int ignoreframes,
  581. int cidmode, const char *modulename)
  582. {
  583. struct cardstate *cs;
  584. unsigned long flags;
  585. int i;
  586. gig_dbg(DEBUG_INIT, "allocating cs");
  587. cs = alloc_cs(drv);
  588. if (!cs) {
  589. pr_err("maximum number of devices exceeded\n");
  590. return NULL;
  591. }
  592. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  593. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  594. if (!cs->bcs) {
  595. pr_err("out of memory\n");
  596. goto error;
  597. }
  598. gig_dbg(DEBUG_INIT, "allocating inbuf");
  599. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  600. if (!cs->inbuf) {
  601. pr_err("out of memory\n");
  602. goto error;
  603. }
  604. cs->cs_init = 0;
  605. cs->channels = channels;
  606. cs->onechannel = onechannel;
  607. cs->ignoreframes = ignoreframes;
  608. INIT_LIST_HEAD(&cs->temp_at_states);
  609. cs->running = 0;
  610. init_timer(&cs->timer); /* clear next & prev */
  611. spin_lock_init(&cs->ev_lock);
  612. cs->ev_tail = 0;
  613. cs->ev_head = 0;
  614. tasklet_init(&cs->event_tasklet, gigaset_handle_event,
  615. (unsigned long) cs);
  616. tty_port_init(&cs->port);
  617. cs->commands_pending = 0;
  618. cs->cur_at_seq = 0;
  619. cs->gotfwver = -1;
  620. cs->dev = NULL;
  621. cs->tty_dev = NULL;
  622. cs->cidmode = cidmode != 0;
  623. cs->tabnocid = gigaset_tab_nocid;
  624. cs->tabcid = gigaset_tab_cid;
  625. init_waitqueue_head(&cs->waitqueue);
  626. cs->waiting = 0;
  627. cs->mode = M_UNKNOWN;
  628. cs->mstate = MS_UNINITIALIZED;
  629. ++cs->cs_init;
  630. gig_dbg(DEBUG_INIT, "setting up at_state");
  631. spin_lock_init(&cs->lock);
  632. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  633. cs->dle = 0;
  634. cs->cbytes = 0;
  635. gig_dbg(DEBUG_INIT, "setting up inbuf");
  636. gigaset_inbuf_init(cs->inbuf, cs);
  637. cs->connected = 0;
  638. cs->isdn_up = 0;
  639. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  640. cs->cmdbuf = cs->lastcmdbuf = NULL;
  641. spin_lock_init(&cs->cmdlock);
  642. cs->curlen = 0;
  643. cs->cmdbytes = 0;
  644. gig_dbg(DEBUG_INIT, "setting up iif");
  645. if (!gigaset_isdn_regdev(cs, modulename)) {
  646. pr_err("error registering ISDN device\n");
  647. goto error;
  648. }
  649. make_valid(cs, VALID_ID);
  650. ++cs->cs_init;
  651. gig_dbg(DEBUG_INIT, "setting up hw");
  652. if (!cs->ops->initcshw(cs))
  653. goto error;
  654. ++cs->cs_init;
  655. /* set up character device */
  656. gigaset_if_init(cs);
  657. /* set up device sysfs */
  658. gigaset_init_dev_sysfs(cs);
  659. /* set up channel data structures */
  660. for (i = 0; i < channels; ++i) {
  661. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  662. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  663. pr_err("could not allocate channel %d data\n", i);
  664. goto error;
  665. }
  666. }
  667. spin_lock_irqsave(&cs->lock, flags);
  668. cs->running = 1;
  669. spin_unlock_irqrestore(&cs->lock, flags);
  670. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  671. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  672. add_timer(&cs->timer);
  673. gig_dbg(DEBUG_INIT, "cs initialized");
  674. return cs;
  675. error:
  676. gig_dbg(DEBUG_INIT, "failed");
  677. gigaset_freecs(cs);
  678. return NULL;
  679. }
  680. EXPORT_SYMBOL_GPL(gigaset_initcs);
  681. /* ReInitialize the b-channel structure on hangup */
  682. void gigaset_bcs_reinit(struct bc_state *bcs)
  683. {
  684. struct sk_buff *skb;
  685. struct cardstate *cs = bcs->cs;
  686. unsigned long flags;
  687. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  688. dev_kfree_skb(skb);
  689. spin_lock_irqsave(&cs->lock, flags);
  690. clear_at_state(&bcs->at_state);
  691. bcs->at_state.ConState = 0;
  692. bcs->at_state.timer_active = 0;
  693. bcs->at_state.timer_expires = 0;
  694. bcs->at_state.cid = -1; /* No CID defined */
  695. spin_unlock_irqrestore(&cs->lock, flags);
  696. bcs->inputstate = 0;
  697. #ifdef CONFIG_GIGASET_DEBUG
  698. bcs->emptycount = 0;
  699. #endif
  700. bcs->rx_fcs = PPP_INITFCS;
  701. bcs->chstate = 0;
  702. bcs->ignore = cs->ignoreframes;
  703. dev_kfree_skb(bcs->rx_skb);
  704. bcs->rx_skb = NULL;
  705. cs->ops->reinitbcshw(bcs);
  706. }
  707. static void cleanup_cs(struct cardstate *cs)
  708. {
  709. struct cmdbuf_t *cb, *tcb;
  710. int i;
  711. unsigned long flags;
  712. spin_lock_irqsave(&cs->lock, flags);
  713. cs->mode = M_UNKNOWN;
  714. cs->mstate = MS_UNINITIALIZED;
  715. clear_at_state(&cs->at_state);
  716. dealloc_at_states(cs);
  717. free_strings(&cs->at_state);
  718. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  719. cs->inbuf->inputstate = INS_command;
  720. cs->inbuf->head = 0;
  721. cs->inbuf->tail = 0;
  722. cb = cs->cmdbuf;
  723. while (cb) {
  724. tcb = cb;
  725. cb = cb->next;
  726. kfree(tcb);
  727. }
  728. cs->cmdbuf = cs->lastcmdbuf = NULL;
  729. cs->curlen = 0;
  730. cs->cmdbytes = 0;
  731. cs->gotfwver = -1;
  732. cs->dle = 0;
  733. cs->cur_at_seq = 0;
  734. cs->commands_pending = 0;
  735. cs->cbytes = 0;
  736. spin_unlock_irqrestore(&cs->lock, flags);
  737. for (i = 0; i < cs->channels; ++i) {
  738. gigaset_freebcs(cs->bcs + i);
  739. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  740. pr_err("could not allocate channel %d data\n", i);
  741. }
  742. if (cs->waiting) {
  743. cs->cmd_result = -ENODEV;
  744. cs->waiting = 0;
  745. wake_up_interruptible(&cs->waitqueue);
  746. }
  747. }
  748. /**
  749. * gigaset_start() - start device operations
  750. * @cs: device descriptor structure.
  751. *
  752. * Prepares the device for use by setting up communication parameters,
  753. * scheduling an EV_START event to initiate device initialization, and
  754. * waiting for completion of the initialization.
  755. *
  756. * Return value:
  757. * 1 - success, 0 - error
  758. */
  759. int gigaset_start(struct cardstate *cs)
  760. {
  761. unsigned long flags;
  762. if (mutex_lock_interruptible(&cs->mutex))
  763. return 0;
  764. spin_lock_irqsave(&cs->lock, flags);
  765. cs->connected = 1;
  766. spin_unlock_irqrestore(&cs->lock, flags);
  767. if (cs->mstate != MS_LOCKED) {
  768. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
  769. cs->ops->baud_rate(cs, B115200);
  770. cs->ops->set_line_ctrl(cs, CS8);
  771. cs->control_state = TIOCM_DTR | TIOCM_RTS;
  772. }
  773. cs->waiting = 1;
  774. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  775. cs->waiting = 0;
  776. goto error;
  777. }
  778. gigaset_schedule_event(cs);
  779. wait_event(cs->waitqueue, !cs->waiting);
  780. mutex_unlock(&cs->mutex);
  781. return 1;
  782. error:
  783. mutex_unlock(&cs->mutex);
  784. return 0;
  785. }
  786. EXPORT_SYMBOL_GPL(gigaset_start);
  787. /**
  788. * gigaset_shutdown() - shut down device operations
  789. * @cs: device descriptor structure.
  790. *
  791. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  792. * waiting for completion of the shutdown.
  793. *
  794. * Return value:
  795. * 0 - success, -1 - error (no device associated)
  796. */
  797. int gigaset_shutdown(struct cardstate *cs)
  798. {
  799. mutex_lock(&cs->mutex);
  800. if (!(cs->flags & VALID_MINOR)) {
  801. mutex_unlock(&cs->mutex);
  802. return -1;
  803. }
  804. cs->waiting = 1;
  805. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
  806. goto exit;
  807. gigaset_schedule_event(cs);
  808. wait_event(cs->waitqueue, !cs->waiting);
  809. cleanup_cs(cs);
  810. exit:
  811. mutex_unlock(&cs->mutex);
  812. return 0;
  813. }
  814. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  815. /**
  816. * gigaset_stop() - stop device operations
  817. * @cs: device descriptor structure.
  818. *
  819. * Stops operations on the device by scheduling an EV_STOP event and
  820. * waiting for completion of the shutdown.
  821. */
  822. void gigaset_stop(struct cardstate *cs)
  823. {
  824. mutex_lock(&cs->mutex);
  825. cs->waiting = 1;
  826. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
  827. goto exit;
  828. gigaset_schedule_event(cs);
  829. wait_event(cs->waitqueue, !cs->waiting);
  830. cleanup_cs(cs);
  831. exit:
  832. mutex_unlock(&cs->mutex);
  833. }
  834. EXPORT_SYMBOL_GPL(gigaset_stop);
  835. static LIST_HEAD(drivers);
  836. static DEFINE_SPINLOCK(driver_lock);
  837. struct cardstate *gigaset_get_cs_by_id(int id)
  838. {
  839. unsigned long flags;
  840. struct cardstate *ret = NULL;
  841. struct cardstate *cs;
  842. struct gigaset_driver *drv;
  843. unsigned i;
  844. spin_lock_irqsave(&driver_lock, flags);
  845. list_for_each_entry(drv, &drivers, list) {
  846. spin_lock(&drv->lock);
  847. for (i = 0; i < drv->minors; ++i) {
  848. cs = drv->cs + i;
  849. if ((cs->flags & VALID_ID) && cs->myid == id) {
  850. ret = cs;
  851. break;
  852. }
  853. }
  854. spin_unlock(&drv->lock);
  855. if (ret)
  856. break;
  857. }
  858. spin_unlock_irqrestore(&driver_lock, flags);
  859. return ret;
  860. }
  861. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  862. {
  863. unsigned long flags;
  864. struct cardstate *ret = NULL;
  865. struct gigaset_driver *drv;
  866. unsigned index;
  867. spin_lock_irqsave(&driver_lock, flags);
  868. list_for_each_entry(drv, &drivers, list) {
  869. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  870. continue;
  871. index = minor - drv->minor;
  872. spin_lock(&drv->lock);
  873. if (drv->cs[index].flags & VALID_MINOR)
  874. ret = drv->cs + index;
  875. spin_unlock(&drv->lock);
  876. if (ret)
  877. break;
  878. }
  879. spin_unlock_irqrestore(&driver_lock, flags);
  880. return ret;
  881. }
  882. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  883. {
  884. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  885. }
  886. /**
  887. * gigaset_freedriver() - free all associated ressources of a driver
  888. * @drv: driver descriptor structure.
  889. *
  890. * Unregisters the driver from the system and deallocates the driver
  891. * structure @drv and all structures referenced from it.
  892. * All devices should be shut down before calling this.
  893. */
  894. void gigaset_freedriver(struct gigaset_driver *drv)
  895. {
  896. unsigned long flags;
  897. spin_lock_irqsave(&driver_lock, flags);
  898. list_del(&drv->list);
  899. spin_unlock_irqrestore(&driver_lock, flags);
  900. gigaset_if_freedriver(drv);
  901. kfree(drv->cs);
  902. kfree(drv);
  903. }
  904. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  905. /**
  906. * gigaset_initdriver() - initialize driver structure
  907. * @minor: First minor number
  908. * @minors: Number of minors this driver can handle
  909. * @procname: Name of the driver
  910. * @devname: Name of the device files (prefix without minor number)
  911. *
  912. * Allocate and initialize gigaset_driver structure. Initialize interface.
  913. *
  914. * Return value:
  915. * Pointer to the gigaset_driver structure on success, NULL on failure.
  916. */
  917. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  918. const char *procname,
  919. const char *devname,
  920. const struct gigaset_ops *ops,
  921. struct module *owner)
  922. {
  923. struct gigaset_driver *drv;
  924. unsigned long flags;
  925. unsigned i;
  926. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  927. if (!drv)
  928. return NULL;
  929. drv->have_tty = 0;
  930. drv->minor = minor;
  931. drv->minors = minors;
  932. spin_lock_init(&drv->lock);
  933. drv->blocked = 0;
  934. drv->ops = ops;
  935. drv->owner = owner;
  936. INIT_LIST_HEAD(&drv->list);
  937. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  938. if (!drv->cs)
  939. goto error;
  940. for (i = 0; i < minors; ++i) {
  941. drv->cs[i].flags = 0;
  942. drv->cs[i].driver = drv;
  943. drv->cs[i].ops = drv->ops;
  944. drv->cs[i].minor_index = i;
  945. mutex_init(&drv->cs[i].mutex);
  946. }
  947. gigaset_if_initdriver(drv, procname, devname);
  948. spin_lock_irqsave(&driver_lock, flags);
  949. list_add(&drv->list, &drivers);
  950. spin_unlock_irqrestore(&driver_lock, flags);
  951. return drv;
  952. error:
  953. kfree(drv->cs);
  954. kfree(drv);
  955. return NULL;
  956. }
  957. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  958. /**
  959. * gigaset_blockdriver() - block driver
  960. * @drv: driver descriptor structure.
  961. *
  962. * Prevents the driver from attaching new devices, in preparation for
  963. * deregistration.
  964. */
  965. void gigaset_blockdriver(struct gigaset_driver *drv)
  966. {
  967. drv->blocked = 1;
  968. }
  969. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  970. static int __init gigaset_init_module(void)
  971. {
  972. /* in accordance with the principle of least astonishment,
  973. * setting the 'debug' parameter to 1 activates a sensible
  974. * set of default debug levels
  975. */
  976. if (gigaset_debuglevel == 1)
  977. gigaset_debuglevel = DEBUG_DEFAULT;
  978. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  979. gigaset_isdn_regdrv();
  980. return 0;
  981. }
  982. static void __exit gigaset_exit_module(void)
  983. {
  984. gigaset_isdn_unregdrv();
  985. }
  986. module_init(gigaset_init_module);
  987. module_exit(gigaset_exit_module);
  988. MODULE_AUTHOR(DRIVER_AUTHOR);
  989. MODULE_DESCRIPTION(DRIVER_DESC);
  990. MODULE_LICENSE("GPL");