common.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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. /**
  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; //FIXME
  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; // FIXME is this a good value?
  119. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  120. return -1; //r
  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. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  132. at_state->timer_index, NULL)) {
  133. //FIXME what should we do?
  134. }
  135. return 1;
  136. }
  137. static void timer_tick(unsigned long data)
  138. {
  139. struct cardstate *cs = (struct cardstate *) data;
  140. unsigned long flags;
  141. unsigned channel;
  142. struct at_state_t *at_state;
  143. int timeout = 0;
  144. spin_lock_irqsave(&cs->lock, flags);
  145. for (channel = 0; channel < cs->channels; ++channel)
  146. if (test_timeout(&cs->bcs[channel].at_state))
  147. timeout = 1;
  148. if (test_timeout(&cs->at_state))
  149. timeout = 1;
  150. list_for_each_entry(at_state, &cs->temp_at_states, list)
  151. if (test_timeout(at_state))
  152. timeout = 1;
  153. if (cs->running) {
  154. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  155. if (timeout) {
  156. gig_dbg(DEBUG_CMD, "scheduling timeout");
  157. tasklet_schedule(&cs->event_tasklet);
  158. }
  159. }
  160. spin_unlock_irqrestore(&cs->lock, flags);
  161. }
  162. int gigaset_get_channel(struct bc_state *bcs)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&bcs->cs->lock, flags);
  166. if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
  167. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  168. bcs->channel);
  169. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  170. return 0;
  171. }
  172. ++bcs->use_count;
  173. bcs->busy = 1;
  174. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  175. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  176. return 1;
  177. }
  178. struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
  179. {
  180. unsigned long flags;
  181. int i;
  182. spin_lock_irqsave(&cs->lock, flags);
  183. if (!try_module_get(cs->driver->owner)) {
  184. gig_dbg(DEBUG_ANY,
  185. "could not get module for allocating channel");
  186. spin_unlock_irqrestore(&cs->lock, flags);
  187. return NULL;
  188. }
  189. for (i = 0; i < cs->channels; ++i)
  190. if (!cs->bcs[i].use_count) {
  191. ++cs->bcs[i].use_count;
  192. cs->bcs[i].busy = 1;
  193. spin_unlock_irqrestore(&cs->lock, flags);
  194. gig_dbg(DEBUG_ANY, "allocated channel %d", i);
  195. return cs->bcs + i;
  196. }
  197. module_put(cs->driver->owner);
  198. spin_unlock_irqrestore(&cs->lock, flags);
  199. gig_dbg(DEBUG_ANY, "no free channel");
  200. return NULL;
  201. }
  202. void gigaset_free_channel(struct bc_state *bcs)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&bcs->cs->lock, flags);
  206. if (!bcs->busy) {
  207. gig_dbg(DEBUG_ANY, "could not free channel %d", bcs->channel);
  208. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  209. return;
  210. }
  211. --bcs->use_count;
  212. bcs->busy = 0;
  213. module_put(bcs->cs->driver->owner);
  214. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  215. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  216. }
  217. int gigaset_get_channels(struct cardstate *cs)
  218. {
  219. unsigned long flags;
  220. int i;
  221. spin_lock_irqsave(&cs->lock, flags);
  222. for (i = 0; i < cs->channels; ++i)
  223. if (cs->bcs[i].use_count) {
  224. spin_unlock_irqrestore(&cs->lock, flags);
  225. gig_dbg(DEBUG_ANY, "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_ANY, "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_ANY, "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_ANY, "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. 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. }
  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. //FIXME cmdbuf
  428. /* fall through */
  429. case 2: /* error in initcshw */
  430. /* Deregister from LL */
  431. make_invalid(cs, VALID_ID);
  432. gigaset_isdn_unregister(cs);
  433. /* fall through */
  434. case 1: /* error when registering to LL */
  435. gig_dbg(DEBUG_INIT, "clearing at_state");
  436. clear_at_state(&cs->at_state);
  437. dealloc_at_states(cs);
  438. /* fall through */
  439. case 0: /* error in basic setup */
  440. clear_events(cs);
  441. gig_dbg(DEBUG_INIT, "freeing inbuf");
  442. kfree(cs->inbuf);
  443. }
  444. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  445. kfree(cs->bcs);
  446. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  447. mutex_unlock(&cs->mutex);
  448. free_cs(cs);
  449. }
  450. EXPORT_SYMBOL_GPL(gigaset_freecs);
  451. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  452. struct cardstate *cs, int cid)
  453. {
  454. int i;
  455. INIT_LIST_HEAD(&at_state->list);
  456. at_state->waiting = 0;
  457. at_state->getstring = 0;
  458. at_state->pending_commands = 0;
  459. at_state->timer_expires = 0;
  460. at_state->timer_active = 0;
  461. at_state->timer_index = 0;
  462. at_state->seq_index = 0;
  463. at_state->ConState = 0;
  464. for (i = 0; i < STR_NUM; ++i)
  465. at_state->str_var[i] = NULL;
  466. at_state->int_var[VAR_ZDLE] = 0;
  467. at_state->int_var[VAR_ZCTP] = -1;
  468. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  469. at_state->cs = cs;
  470. at_state->bcs = bcs;
  471. at_state->cid = cid;
  472. if (!cid)
  473. at_state->replystruct = cs->tabnocid;
  474. else
  475. at_state->replystruct = cs->tabcid;
  476. }
  477. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
  478. /* inbuf->read must be allocated before! */
  479. {
  480. inbuf->head = 0;
  481. inbuf->tail = 0;
  482. inbuf->cs = cs;
  483. inbuf->inputstate = INS_command;
  484. }
  485. /**
  486. * gigaset_fill_inbuf() - append received data to input buffer
  487. * @inbuf: buffer structure.
  488. * @src: received data.
  489. * @numbytes: number of bytes received.
  490. */
  491. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  492. unsigned numbytes)
  493. {
  494. unsigned n, head, tail, bytesleft;
  495. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  496. if (!numbytes)
  497. return 0;
  498. bytesleft = numbytes;
  499. tail = inbuf->tail;
  500. head = inbuf->head;
  501. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  502. while (bytesleft) {
  503. if (head > tail)
  504. n = head - 1 - tail;
  505. else if (head == 0)
  506. n = (RBUFSIZE-1) - tail;
  507. else
  508. n = RBUFSIZE - tail;
  509. if (!n) {
  510. dev_err(inbuf->cs->dev,
  511. "buffer overflow (%u bytes lost)\n",
  512. bytesleft);
  513. break;
  514. }
  515. if (n > bytesleft)
  516. n = bytesleft;
  517. memcpy(inbuf->data + tail, src, n);
  518. bytesleft -= n;
  519. tail = (tail + n) % RBUFSIZE;
  520. src += n;
  521. }
  522. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  523. inbuf->tail = tail;
  524. return numbytes != bytesleft;
  525. }
  526. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  527. /* Initialize the b-channel structure */
  528. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  529. struct cardstate *cs, int channel)
  530. {
  531. int i;
  532. bcs->tx_skb = NULL; //FIXME -> hw part
  533. skb_queue_head_init(&bcs->squeue);
  534. bcs->corrupted = 0;
  535. bcs->trans_down = 0;
  536. bcs->trans_up = 0;
  537. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  538. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  539. #ifdef CONFIG_GIGASET_DEBUG
  540. bcs->emptycount = 0;
  541. #endif
  542. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  543. bcs->fcs = PPP_INITFCS;
  544. bcs->inputstate = 0;
  545. if (cs->ignoreframes) {
  546. bcs->skb = NULL;
  547. } else {
  548. bcs->skb = dev_alloc_skb(SBUFSIZE + cs->hw_hdr_len);
  549. if (bcs->skb != NULL)
  550. skb_reserve(bcs->skb, cs->hw_hdr_len);
  551. else
  552. pr_err("out of memory\n");
  553. }
  554. bcs->channel = channel;
  555. bcs->cs = cs;
  556. bcs->chstate = 0;
  557. bcs->use_count = 1;
  558. bcs->busy = 0;
  559. bcs->ignore = cs->ignoreframes;
  560. for (i = 0; i < AT_NUM; ++i)
  561. bcs->commands[i] = NULL;
  562. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  563. if (cs->ops->initbcshw(bcs))
  564. return bcs;
  565. gig_dbg(DEBUG_INIT, " failed");
  566. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  567. dev_kfree_skb(bcs->skb);
  568. bcs->skb = NULL;
  569. return NULL;
  570. }
  571. /**
  572. * gigaset_initcs() - initialize device structure
  573. * @drv: hardware driver the device belongs to
  574. * @channels: number of B channels supported by device
  575. * @onechannel: !=0 if B channel data and AT commands share one
  576. * communication channel (M10x),
  577. * ==0 if B channels have separate communication channels (base)
  578. * @ignoreframes: number of frames to ignore after setting up B channel
  579. * @cidmode: !=0: start in CallID mode
  580. * @modulename: name of driver module for LL registration
  581. *
  582. * Allocate and initialize cardstate structure for Gigaset driver
  583. * Calls hardware dependent gigaset_initcshw() function
  584. * Calls B channel initialization function gigaset_initbcs() for each B channel
  585. *
  586. * Return value:
  587. * pointer to cardstate structure
  588. */
  589. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  590. int onechannel, int ignoreframes,
  591. int cidmode, const char *modulename)
  592. {
  593. struct cardstate *cs = NULL;
  594. unsigned long flags;
  595. int i;
  596. gig_dbg(DEBUG_INIT, "allocating cs");
  597. if (!(cs = alloc_cs(drv))) {
  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_register(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. } else {
  787. //FIXME use some saved values?
  788. }
  789. cs->waiting = 1;
  790. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  791. cs->waiting = 0;
  792. //FIXME what should we do?
  793. goto error;
  794. }
  795. gig_dbg(DEBUG_CMD, "scheduling START");
  796. gigaset_schedule_event(cs);
  797. wait_event(cs->waitqueue, !cs->waiting);
  798. mutex_unlock(&cs->mutex);
  799. return 1;
  800. error:
  801. mutex_unlock(&cs->mutex);
  802. return 0;
  803. }
  804. EXPORT_SYMBOL_GPL(gigaset_start);
  805. /**
  806. * gigaset_shutdown() - shut down device operations
  807. * @cs: device descriptor structure.
  808. *
  809. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  810. * waiting for completion of the shutdown.
  811. *
  812. * Return value:
  813. * 0 - success, -1 - error (no device associated)
  814. */
  815. int gigaset_shutdown(struct cardstate *cs)
  816. {
  817. mutex_lock(&cs->mutex);
  818. if (!(cs->flags & VALID_MINOR)) {
  819. mutex_unlock(&cs->mutex);
  820. return -1;
  821. }
  822. cs->waiting = 1;
  823. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  824. //FIXME what should we do?
  825. goto exit;
  826. }
  827. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  828. gigaset_schedule_event(cs);
  829. wait_event(cs->waitqueue, !cs->waiting);
  830. cleanup_cs(cs);
  831. exit:
  832. mutex_unlock(&cs->mutex);
  833. return 0;
  834. }
  835. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  836. /**
  837. * gigaset_stop() - stop device operations
  838. * @cs: device descriptor structure.
  839. *
  840. * Stops operations on the device by scheduling an EV_STOP event and
  841. * waiting for completion of the shutdown.
  842. */
  843. void gigaset_stop(struct cardstate *cs)
  844. {
  845. mutex_lock(&cs->mutex);
  846. cs->waiting = 1;
  847. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  848. //FIXME what should we do?
  849. goto exit;
  850. }
  851. gig_dbg(DEBUG_CMD, "scheduling STOP");
  852. gigaset_schedule_event(cs);
  853. wait_event(cs->waitqueue, !cs->waiting);
  854. cleanup_cs(cs);
  855. exit:
  856. mutex_unlock(&cs->mutex);
  857. }
  858. EXPORT_SYMBOL_GPL(gigaset_stop);
  859. static LIST_HEAD(drivers);
  860. static DEFINE_SPINLOCK(driver_lock);
  861. struct cardstate *gigaset_get_cs_by_id(int id)
  862. {
  863. unsigned long flags;
  864. struct cardstate *ret = NULL;
  865. struct cardstate *cs;
  866. struct gigaset_driver *drv;
  867. unsigned i;
  868. spin_lock_irqsave(&driver_lock, flags);
  869. list_for_each_entry(drv, &drivers, list) {
  870. spin_lock(&drv->lock);
  871. for (i = 0; i < drv->minors; ++i) {
  872. cs = drv->cs + i;
  873. if ((cs->flags & VALID_ID) && cs->myid == id) {
  874. ret = cs;
  875. break;
  876. }
  877. }
  878. spin_unlock(&drv->lock);
  879. if (ret)
  880. break;
  881. }
  882. spin_unlock_irqrestore(&driver_lock, flags);
  883. return ret;
  884. }
  885. void gigaset_debugdrivers(void)
  886. {
  887. unsigned long flags;
  888. static struct cardstate *cs;
  889. struct gigaset_driver *drv;
  890. unsigned i;
  891. spin_lock_irqsave(&driver_lock, flags);
  892. list_for_each_entry(drv, &drivers, list) {
  893. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  894. spin_lock(&drv->lock);
  895. for (i = 0; i < drv->minors; ++i) {
  896. gig_dbg(DEBUG_DRIVER, " index %u", i);
  897. cs = drv->cs + i;
  898. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  899. gig_dbg(DEBUG_DRIVER, " flags 0x%02x", cs->flags);
  900. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  901. cs->minor_index);
  902. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  903. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  904. }
  905. spin_unlock(&drv->lock);
  906. }
  907. spin_unlock_irqrestore(&driver_lock, flags);
  908. }
  909. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  910. {
  911. unsigned long flags;
  912. struct cardstate *ret = NULL;
  913. struct gigaset_driver *drv;
  914. unsigned index;
  915. spin_lock_irqsave(&driver_lock, flags);
  916. list_for_each_entry(drv, &drivers, list) {
  917. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  918. continue;
  919. index = minor - drv->minor;
  920. spin_lock(&drv->lock);
  921. if (drv->cs[index].flags & VALID_MINOR)
  922. ret = drv->cs + index;
  923. spin_unlock(&drv->lock);
  924. if (ret)
  925. break;
  926. }
  927. spin_unlock_irqrestore(&driver_lock, flags);
  928. return ret;
  929. }
  930. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  931. {
  932. if (tty->index < 0 || tty->index >= tty->driver->num)
  933. return NULL;
  934. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  935. }
  936. /**
  937. * gigaset_freedriver() - free all associated ressources of a driver
  938. * @drv: driver descriptor structure.
  939. *
  940. * Unregisters the driver from the system and deallocates the driver
  941. * structure @drv and all structures referenced from it.
  942. * All devices should be shut down before calling this.
  943. */
  944. void gigaset_freedriver(struct gigaset_driver *drv)
  945. {
  946. unsigned long flags;
  947. spin_lock_irqsave(&driver_lock, flags);
  948. list_del(&drv->list);
  949. spin_unlock_irqrestore(&driver_lock, flags);
  950. gigaset_if_freedriver(drv);
  951. kfree(drv->cs);
  952. kfree(drv);
  953. }
  954. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  955. /**
  956. * gigaset_initdriver() - initialize driver structure
  957. * @minor: First minor number
  958. * @minors: Number of minors this driver can handle
  959. * @procname: Name of the driver
  960. * @devname: Name of the device files (prefix without minor number)
  961. *
  962. * Allocate and initialize gigaset_driver structure. Initialize interface.
  963. *
  964. * Return value:
  965. * Pointer to the gigaset_driver structure on success, NULL on failure.
  966. */
  967. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  968. const char *procname,
  969. const char *devname,
  970. const struct gigaset_ops *ops,
  971. struct module *owner)
  972. {
  973. struct gigaset_driver *drv;
  974. unsigned long flags;
  975. unsigned i;
  976. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  977. if (!drv)
  978. return NULL;
  979. drv->have_tty = 0;
  980. drv->minor = minor;
  981. drv->minors = minors;
  982. spin_lock_init(&drv->lock);
  983. drv->blocked = 0;
  984. drv->ops = ops;
  985. drv->owner = owner;
  986. INIT_LIST_HEAD(&drv->list);
  987. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  988. if (!drv->cs)
  989. goto error;
  990. for (i = 0; i < minors; ++i) {
  991. drv->cs[i].flags = 0;
  992. drv->cs[i].driver = drv;
  993. drv->cs[i].ops = drv->ops;
  994. drv->cs[i].minor_index = i;
  995. mutex_init(&drv->cs[i].mutex);
  996. }
  997. gigaset_if_initdriver(drv, procname, devname);
  998. spin_lock_irqsave(&driver_lock, flags);
  999. list_add(&drv->list, &drivers);
  1000. spin_unlock_irqrestore(&driver_lock, flags);
  1001. return drv;
  1002. error:
  1003. kfree(drv->cs);
  1004. kfree(drv);
  1005. return NULL;
  1006. }
  1007. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  1008. /**
  1009. * gigaset_blockdriver() - block driver
  1010. * @drv: driver descriptor structure.
  1011. *
  1012. * Prevents the driver from attaching new devices, in preparation for
  1013. * deregistration.
  1014. */
  1015. void gigaset_blockdriver(struct gigaset_driver *drv)
  1016. {
  1017. drv->blocked = 1;
  1018. }
  1019. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  1020. static int __init gigaset_init_module(void)
  1021. {
  1022. /* in accordance with the principle of least astonishment,
  1023. * setting the 'debug' parameter to 1 activates a sensible
  1024. * set of default debug levels
  1025. */
  1026. if (gigaset_debuglevel == 1)
  1027. gigaset_debuglevel = DEBUG_DEFAULT;
  1028. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  1029. return 0;
  1030. }
  1031. static void __exit gigaset_exit_module(void)
  1032. {
  1033. }
  1034. module_init(gigaset_init_module);
  1035. module_exit(gigaset_exit_module);
  1036. MODULE_AUTHOR(DRIVER_AUTHOR);
  1037. MODULE_DESCRIPTION(DRIVER_DESC);
  1038. MODULE_LICENSE("GPL");