common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. cs->commands_pending = 0;
  617. cs->cur_at_seq = 0;
  618. cs->gotfwver = -1;
  619. cs->open_count = 0;
  620. cs->dev = NULL;
  621. cs->tty = NULL;
  622. cs->tty_dev = NULL;
  623. cs->cidmode = cidmode != 0;
  624. cs->tabnocid = gigaset_tab_nocid;
  625. cs->tabcid = gigaset_tab_cid;
  626. init_waitqueue_head(&cs->waitqueue);
  627. cs->waiting = 0;
  628. cs->mode = M_UNKNOWN;
  629. cs->mstate = MS_UNINITIALIZED;
  630. ++cs->cs_init;
  631. gig_dbg(DEBUG_INIT, "setting up at_state");
  632. spin_lock_init(&cs->lock);
  633. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  634. cs->dle = 0;
  635. cs->cbytes = 0;
  636. gig_dbg(DEBUG_INIT, "setting up inbuf");
  637. gigaset_inbuf_init(cs->inbuf, cs);
  638. cs->connected = 0;
  639. cs->isdn_up = 0;
  640. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  641. cs->cmdbuf = cs->lastcmdbuf = NULL;
  642. spin_lock_init(&cs->cmdlock);
  643. cs->curlen = 0;
  644. cs->cmdbytes = 0;
  645. gig_dbg(DEBUG_INIT, "setting up iif");
  646. if (!gigaset_isdn_regdev(cs, modulename)) {
  647. pr_err("error registering ISDN device\n");
  648. goto error;
  649. }
  650. make_valid(cs, VALID_ID);
  651. ++cs->cs_init;
  652. gig_dbg(DEBUG_INIT, "setting up hw");
  653. if (!cs->ops->initcshw(cs))
  654. goto error;
  655. ++cs->cs_init;
  656. /* set up character device */
  657. gigaset_if_init(cs);
  658. /* set up device sysfs */
  659. gigaset_init_dev_sysfs(cs);
  660. /* set up channel data structures */
  661. for (i = 0; i < channels; ++i) {
  662. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  663. if (!gigaset_initbcs(cs->bcs + i, cs, i)) {
  664. pr_err("could not allocate channel %d data\n", i);
  665. goto error;
  666. }
  667. }
  668. spin_lock_irqsave(&cs->lock, flags);
  669. cs->running = 1;
  670. spin_unlock_irqrestore(&cs->lock, flags);
  671. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  672. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  673. add_timer(&cs->timer);
  674. gig_dbg(DEBUG_INIT, "cs initialized");
  675. return cs;
  676. error:
  677. gig_dbg(DEBUG_INIT, "failed");
  678. gigaset_freecs(cs);
  679. return NULL;
  680. }
  681. EXPORT_SYMBOL_GPL(gigaset_initcs);
  682. /* ReInitialize the b-channel structure on hangup */
  683. void gigaset_bcs_reinit(struct bc_state *bcs)
  684. {
  685. struct sk_buff *skb;
  686. struct cardstate *cs = bcs->cs;
  687. unsigned long flags;
  688. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  689. dev_kfree_skb(skb);
  690. spin_lock_irqsave(&cs->lock, flags);
  691. clear_at_state(&bcs->at_state);
  692. bcs->at_state.ConState = 0;
  693. bcs->at_state.timer_active = 0;
  694. bcs->at_state.timer_expires = 0;
  695. bcs->at_state.cid = -1; /* No CID defined */
  696. spin_unlock_irqrestore(&cs->lock, flags);
  697. bcs->inputstate = 0;
  698. #ifdef CONFIG_GIGASET_DEBUG
  699. bcs->emptycount = 0;
  700. #endif
  701. bcs->rx_fcs = PPP_INITFCS;
  702. bcs->chstate = 0;
  703. bcs->ignore = cs->ignoreframes;
  704. dev_kfree_skb(bcs->rx_skb);
  705. bcs->rx_skb = NULL;
  706. cs->ops->reinitbcshw(bcs);
  707. }
  708. static void cleanup_cs(struct cardstate *cs)
  709. {
  710. struct cmdbuf_t *cb, *tcb;
  711. int i;
  712. unsigned long flags;
  713. spin_lock_irqsave(&cs->lock, flags);
  714. cs->mode = M_UNKNOWN;
  715. cs->mstate = MS_UNINITIALIZED;
  716. clear_at_state(&cs->at_state);
  717. dealloc_at_states(cs);
  718. free_strings(&cs->at_state);
  719. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  720. cs->inbuf->inputstate = INS_command;
  721. cs->inbuf->head = 0;
  722. cs->inbuf->tail = 0;
  723. cb = cs->cmdbuf;
  724. while (cb) {
  725. tcb = cb;
  726. cb = cb->next;
  727. kfree(tcb);
  728. }
  729. cs->cmdbuf = cs->lastcmdbuf = NULL;
  730. cs->curlen = 0;
  731. cs->cmdbytes = 0;
  732. cs->gotfwver = -1;
  733. cs->dle = 0;
  734. cs->cur_at_seq = 0;
  735. cs->commands_pending = 0;
  736. cs->cbytes = 0;
  737. spin_unlock_irqrestore(&cs->lock, flags);
  738. for (i = 0; i < cs->channels; ++i) {
  739. gigaset_freebcs(cs->bcs + i);
  740. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  741. pr_err("could not allocate channel %d data\n", i);
  742. }
  743. if (cs->waiting) {
  744. cs->cmd_result = -ENODEV;
  745. cs->waiting = 0;
  746. wake_up_interruptible(&cs->waitqueue);
  747. }
  748. }
  749. /**
  750. * gigaset_start() - start device operations
  751. * @cs: device descriptor structure.
  752. *
  753. * Prepares the device for use by setting up communication parameters,
  754. * scheduling an EV_START event to initiate device initialization, and
  755. * waiting for completion of the initialization.
  756. *
  757. * Return value:
  758. * 1 - success, 0 - error
  759. */
  760. int gigaset_start(struct cardstate *cs)
  761. {
  762. unsigned long flags;
  763. if (mutex_lock_interruptible(&cs->mutex))
  764. return 0;
  765. spin_lock_irqsave(&cs->lock, flags);
  766. cs->connected = 1;
  767. spin_unlock_irqrestore(&cs->lock, flags);
  768. if (cs->mstate != MS_LOCKED) {
  769. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  770. cs->ops->baud_rate(cs, B115200);
  771. cs->ops->set_line_ctrl(cs, CS8);
  772. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  773. }
  774. cs->waiting = 1;
  775. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  776. cs->waiting = 0;
  777. goto error;
  778. }
  779. gigaset_schedule_event(cs);
  780. wait_event(cs->waitqueue, !cs->waiting);
  781. mutex_unlock(&cs->mutex);
  782. return 1;
  783. error:
  784. mutex_unlock(&cs->mutex);
  785. return 0;
  786. }
  787. EXPORT_SYMBOL_GPL(gigaset_start);
  788. /**
  789. * gigaset_shutdown() - shut down device operations
  790. * @cs: device descriptor structure.
  791. *
  792. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  793. * waiting for completion of the shutdown.
  794. *
  795. * Return value:
  796. * 0 - success, -1 - error (no device associated)
  797. */
  798. int gigaset_shutdown(struct cardstate *cs)
  799. {
  800. mutex_lock(&cs->mutex);
  801. if (!(cs->flags & VALID_MINOR)) {
  802. mutex_unlock(&cs->mutex);
  803. return -1;
  804. }
  805. cs->waiting = 1;
  806. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
  807. goto exit;
  808. gigaset_schedule_event(cs);
  809. wait_event(cs->waitqueue, !cs->waiting);
  810. cleanup_cs(cs);
  811. exit:
  812. mutex_unlock(&cs->mutex);
  813. return 0;
  814. }
  815. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  816. /**
  817. * gigaset_stop() - stop device operations
  818. * @cs: device descriptor structure.
  819. *
  820. * Stops operations on the device by scheduling an EV_STOP event and
  821. * waiting for completion of the shutdown.
  822. */
  823. void gigaset_stop(struct cardstate *cs)
  824. {
  825. mutex_lock(&cs->mutex);
  826. cs->waiting = 1;
  827. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
  828. goto exit;
  829. gigaset_schedule_event(cs);
  830. wait_event(cs->waitqueue, !cs->waiting);
  831. cleanup_cs(cs);
  832. exit:
  833. mutex_unlock(&cs->mutex);
  834. }
  835. EXPORT_SYMBOL_GPL(gigaset_stop);
  836. static LIST_HEAD(drivers);
  837. static DEFINE_SPINLOCK(driver_lock);
  838. struct cardstate *gigaset_get_cs_by_id(int id)
  839. {
  840. unsigned long flags;
  841. struct cardstate *ret = NULL;
  842. struct cardstate *cs;
  843. struct gigaset_driver *drv;
  844. unsigned i;
  845. spin_lock_irqsave(&driver_lock, flags);
  846. list_for_each_entry(drv, &drivers, list) {
  847. spin_lock(&drv->lock);
  848. for (i = 0; i < drv->minors; ++i) {
  849. cs = drv->cs + i;
  850. if ((cs->flags & VALID_ID) && cs->myid == id) {
  851. ret = cs;
  852. break;
  853. }
  854. }
  855. spin_unlock(&drv->lock);
  856. if (ret)
  857. break;
  858. }
  859. spin_unlock_irqrestore(&driver_lock, flags);
  860. return ret;
  861. }
  862. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  863. {
  864. unsigned long flags;
  865. struct cardstate *ret = NULL;
  866. struct gigaset_driver *drv;
  867. unsigned index;
  868. spin_lock_irqsave(&driver_lock, flags);
  869. list_for_each_entry(drv, &drivers, list) {
  870. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  871. continue;
  872. index = minor - drv->minor;
  873. spin_lock(&drv->lock);
  874. if (drv->cs[index].flags & VALID_MINOR)
  875. ret = drv->cs + index;
  876. spin_unlock(&drv->lock);
  877. if (ret)
  878. break;
  879. }
  880. spin_unlock_irqrestore(&driver_lock, flags);
  881. return ret;
  882. }
  883. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  884. {
  885. if (tty->index < 0 || tty->index >= tty->driver->num)
  886. return NULL;
  887. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  888. }
  889. /**
  890. * gigaset_freedriver() - free all associated ressources of a driver
  891. * @drv: driver descriptor structure.
  892. *
  893. * Unregisters the driver from the system and deallocates the driver
  894. * structure @drv and all structures referenced from it.
  895. * All devices should be shut down before calling this.
  896. */
  897. void gigaset_freedriver(struct gigaset_driver *drv)
  898. {
  899. unsigned long flags;
  900. spin_lock_irqsave(&driver_lock, flags);
  901. list_del(&drv->list);
  902. spin_unlock_irqrestore(&driver_lock, flags);
  903. gigaset_if_freedriver(drv);
  904. kfree(drv->cs);
  905. kfree(drv);
  906. }
  907. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  908. /**
  909. * gigaset_initdriver() - initialize driver structure
  910. * @minor: First minor number
  911. * @minors: Number of minors this driver can handle
  912. * @procname: Name of the driver
  913. * @devname: Name of the device files (prefix without minor number)
  914. *
  915. * Allocate and initialize gigaset_driver structure. Initialize interface.
  916. *
  917. * Return value:
  918. * Pointer to the gigaset_driver structure on success, NULL on failure.
  919. */
  920. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  921. const char *procname,
  922. const char *devname,
  923. const struct gigaset_ops *ops,
  924. struct module *owner)
  925. {
  926. struct gigaset_driver *drv;
  927. unsigned long flags;
  928. unsigned i;
  929. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  930. if (!drv)
  931. return NULL;
  932. drv->have_tty = 0;
  933. drv->minor = minor;
  934. drv->minors = minors;
  935. spin_lock_init(&drv->lock);
  936. drv->blocked = 0;
  937. drv->ops = ops;
  938. drv->owner = owner;
  939. INIT_LIST_HEAD(&drv->list);
  940. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  941. if (!drv->cs)
  942. goto error;
  943. for (i = 0; i < minors; ++i) {
  944. drv->cs[i].flags = 0;
  945. drv->cs[i].driver = drv;
  946. drv->cs[i].ops = drv->ops;
  947. drv->cs[i].minor_index = i;
  948. mutex_init(&drv->cs[i].mutex);
  949. }
  950. gigaset_if_initdriver(drv, procname, devname);
  951. spin_lock_irqsave(&driver_lock, flags);
  952. list_add(&drv->list, &drivers);
  953. spin_unlock_irqrestore(&driver_lock, flags);
  954. return drv;
  955. error:
  956. kfree(drv->cs);
  957. kfree(drv);
  958. return NULL;
  959. }
  960. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  961. /**
  962. * gigaset_blockdriver() - block driver
  963. * @drv: driver descriptor structure.
  964. *
  965. * Prevents the driver from attaching new devices, in preparation for
  966. * deregistration.
  967. */
  968. void gigaset_blockdriver(struct gigaset_driver *drv)
  969. {
  970. drv->blocked = 1;
  971. }
  972. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  973. static int __init gigaset_init_module(void)
  974. {
  975. /* in accordance with the principle of least astonishment,
  976. * setting the 'debug' parameter to 1 activates a sensible
  977. * set of default debug levels
  978. */
  979. if (gigaset_debuglevel == 1)
  980. gigaset_debuglevel = DEBUG_DEFAULT;
  981. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  982. gigaset_isdn_regdrv();
  983. return 0;
  984. }
  985. static void __exit gigaset_exit_module(void)
  986. {
  987. gigaset_isdn_unregdrv();
  988. }
  989. module_init(gigaset_init_module);
  990. module_exit(gigaset_exit_module);
  991. MODULE_AUTHOR(DRIVER_AUTHOR);
  992. MODULE_DESCRIPTION(DRIVER_DESC);
  993. MODULE_LICENSE("GPL");