common.c 28 KB

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