common.c 25 KB

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