usb-gigaset.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * USB driver for Gigaset 307x directly or using M105 Data.
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers
  5. * and Hansjoerg Lipp <hjlipp@web.de>.
  6. *
  7. * This driver was derived from the USB skeleton driver by
  8. * Greg Kroah-Hartman <greg@kroah.com>
  9. *
  10. * =====================================================================
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. * =====================================================================
  16. */
  17. #include "gigaset.h"
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/usb.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. /* Version Information */
  25. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  26. #define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
  27. /* Module parameters */
  28. static int startmode = SM_ISDN;
  29. static int cidmode = 1;
  30. module_param(startmode, int, S_IRUGO);
  31. module_param(cidmode, int, S_IRUGO);
  32. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  33. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  34. #define GIGASET_MINORS 1
  35. #define GIGASET_MINOR 8
  36. #define GIGASET_MODULENAME "usb_gigaset"
  37. #define GIGASET_DEVNAME "ttyGU"
  38. #define IF_WRITEBUF 2000 //FIXME // WAKEUP_CHARS: 256
  39. /* Values for the Gigaset M105 Data */
  40. #define USB_M105_VENDOR_ID 0x0681
  41. #define USB_M105_PRODUCT_ID 0x0009
  42. /* table of devices that work with this driver */
  43. static struct usb_device_id gigaset_table [] = {
  44. { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
  45. { } /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(usb, gigaset_table);
  48. /*
  49. * Control requests (empty fields: 00)
  50. *
  51. * RT|RQ|VALUE|INDEX|LEN |DATA
  52. * In:
  53. * C1 08 01
  54. * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
  55. * C1 0F ll ll
  56. * Get device information/status (llll: 0x200 and 0x40 seen).
  57. * Real size: I only saw MIN(llll,0x64).
  58. * Contents: seems to be always the same...
  59. * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
  60. * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
  61. * rest: ?
  62. * Out:
  63. * 41 11
  64. * Initialize/reset device ?
  65. * 41 00 xx 00
  66. * ? (xx=00 or 01; 01 on start, 00 on close)
  67. * 41 07 vv mm
  68. * Set/clear flags vv=value, mm=mask (see RQ 08)
  69. * 41 12 xx
  70. * Used before the following configuration requests are issued
  71. * (with xx=0x0f). I've seen other values<0xf, though.
  72. * 41 01 xx xx
  73. * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
  74. * 41 03 ps bb
  75. * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
  76. * [ 0x30: m, 0x40: s ]
  77. * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
  78. * bb: bits/byte (seen 7 and 8)
  79. * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
  80. * ??
  81. * Initialization: 01, 40, 00, 00
  82. * Open device: 00 40, 00, 00
  83. * yy and zz seem to be equal, either 0x00 or 0x0a
  84. * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
  85. * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
  86. * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
  87. * xx is usually 0x00 but was 0x7e before starting data transfer
  88. * in unimodem mode. So, this might be an array of characters that need
  89. * special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
  90. *
  91. * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
  92. * flags per packet.
  93. */
  94. static int gigaset_probe(struct usb_interface *interface,
  95. const struct usb_device_id *id);
  96. static void gigaset_disconnect(struct usb_interface *interface);
  97. static struct gigaset_driver *driver = NULL;
  98. static struct cardstate *cardstate = NULL;
  99. /* usb specific object needed to register this driver with the usb subsystem */
  100. static struct usb_driver gigaset_usb_driver = {
  101. .name = GIGASET_MODULENAME,
  102. .probe = gigaset_probe,
  103. .disconnect = gigaset_disconnect,
  104. .id_table = gigaset_table,
  105. };
  106. struct usb_cardstate {
  107. struct usb_device *udev; /* usb device pointer */
  108. struct usb_interface *interface; /* interface for this device */
  109. atomic_t busy; /* bulk output in progress */
  110. /* Output buffer */
  111. unsigned char *bulk_out_buffer;
  112. int bulk_out_size;
  113. __u8 bulk_out_endpointAddr;
  114. struct urb *bulk_out_urb;
  115. /* Input buffer */
  116. int rcvbuf_size;
  117. struct urb *read_urb;
  118. __u8 int_in_endpointAddr;
  119. char bchars[6]; /* for request 0x19 */
  120. };
  121. struct usb_bc_state {};
  122. static inline unsigned tiocm_to_gigaset(unsigned state)
  123. {
  124. return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
  125. }
  126. #ifdef CONFIG_GIGASET_UNDOCREQ
  127. /* WARNING: EXPERIMENTAL! */
  128. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  129. unsigned new_state)
  130. {
  131. struct usb_device *udev = cs->hw.usb->udev;
  132. unsigned mask, val;
  133. int r;
  134. mask = tiocm_to_gigaset(old_state ^ new_state);
  135. val = tiocm_to_gigaset(new_state);
  136. gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
  137. // don't use this in an interrupt/BH
  138. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
  139. (val & 0xff) | ((mask & 0xff) << 8), 0,
  140. NULL, 0, 2000 /* timeout? */);
  141. if (r < 0)
  142. return r;
  143. //..
  144. return 0;
  145. }
  146. static int set_value(struct cardstate *cs, u8 req, u16 val)
  147. {
  148. struct usb_device *udev = cs->hw.usb->udev;
  149. int r, r2;
  150. gig_dbg(DEBUG_USBREQ, "request %02x (%04x)",
  151. (unsigned)req, (unsigned)val);
  152. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41,
  153. 0xf /*?*/, 0, NULL, 0, 2000 /*?*/);
  154. /* no idea what this does */
  155. if (r < 0) {
  156. dev_err(&udev->dev, "error %d on request 0x12\n", -r);
  157. return r;
  158. }
  159. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
  160. val, 0, NULL, 0, 2000 /*?*/);
  161. if (r < 0)
  162. dev_err(&udev->dev, "error %d on request 0x%02x\n",
  163. -r, (unsigned)req);
  164. r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  165. 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
  166. if (r2 < 0)
  167. dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
  168. return r < 0 ? r : (r2 < 0 ? r2 : 0);
  169. }
  170. /* WARNING: HIGHLY EXPERIMENTAL! */
  171. // don't use this in an interrupt/BH
  172. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  173. {
  174. u16 val;
  175. u32 rate;
  176. cflag &= CBAUD;
  177. switch (cflag) {
  178. //FIXME more values?
  179. case B300: rate = 300; break;
  180. case B600: rate = 600; break;
  181. case B1200: rate = 1200; break;
  182. case B2400: rate = 2400; break;
  183. case B4800: rate = 4800; break;
  184. case B9600: rate = 9600; break;
  185. case B19200: rate = 19200; break;
  186. case B38400: rate = 38400; break;
  187. case B57600: rate = 57600; break;
  188. case B115200: rate = 115200; break;
  189. default:
  190. rate = 9600;
  191. dev_err(cs->dev, "unsupported baudrate request 0x%x,"
  192. " using default of B9600\n", cflag);
  193. }
  194. val = 0x383fff / rate + 1;
  195. return set_value(cs, 1, val);
  196. }
  197. /* WARNING: HIGHLY EXPERIMENTAL! */
  198. // don't use this in an interrupt/BH
  199. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  200. {
  201. u16 val = 0;
  202. /* set the parity */
  203. if (cflag & PARENB)
  204. val |= (cflag & PARODD) ? 0x10 : 0x20;
  205. /* set the number of data bits */
  206. switch (cflag & CSIZE) {
  207. case CS5:
  208. val |= 5 << 8; break;
  209. case CS6:
  210. val |= 6 << 8; break;
  211. case CS7:
  212. val |= 7 << 8; break;
  213. case CS8:
  214. val |= 8 << 8; break;
  215. default:
  216. dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
  217. val |= 8 << 8;
  218. break;
  219. }
  220. /* set the number of stop bits */
  221. if (cflag & CSTOPB) {
  222. if ((cflag & CSIZE) == CS5)
  223. val |= 1; /* 1.5 stop bits */ //FIXME is this okay?
  224. else
  225. val |= 2; /* 2 stop bits */
  226. }
  227. return set_value(cs, 3, val);
  228. }
  229. #else
  230. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  231. unsigned new_state)
  232. {
  233. return -EINVAL;
  234. }
  235. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  236. {
  237. return -EINVAL;
  238. }
  239. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  240. {
  241. return -EINVAL;
  242. }
  243. #endif
  244. /*================================================================================================================*/
  245. static int gigaset_init_bchannel(struct bc_state *bcs)
  246. {
  247. /* nothing to do for M10x */
  248. gigaset_bchannel_up(bcs);
  249. return 0;
  250. }
  251. static int gigaset_close_bchannel(struct bc_state *bcs)
  252. {
  253. /* nothing to do for M10x */
  254. gigaset_bchannel_down(bcs);
  255. return 0;
  256. }
  257. static int write_modem(struct cardstate *cs);
  258. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
  259. /* Write tasklet handler: Continue sending current skb, or send command, or
  260. * start sending an skb from the send queue.
  261. */
  262. static void gigaset_modem_fill(unsigned long data)
  263. {
  264. struct cardstate *cs = (struct cardstate *) data;
  265. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  266. struct cmdbuf_t *cb;
  267. unsigned long flags;
  268. int again;
  269. gig_dbg(DEBUG_OUTPUT, "modem_fill");
  270. if (atomic_read(&cs->hw.usb->busy)) {
  271. gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
  272. return;
  273. }
  274. do {
  275. again = 0;
  276. if (!bcs->tx_skb) { /* no skb is being sent */
  277. spin_lock_irqsave(&cs->cmdlock, flags);
  278. cb = cs->cmdbuf;
  279. spin_unlock_irqrestore(&cs->cmdlock, flags);
  280. if (cb) { /* commands to send? */
  281. gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
  282. if (send_cb(cs, cb) < 0) {
  283. gig_dbg(DEBUG_OUTPUT,
  284. "modem_fill: send_cb failed");
  285. again = 1; /* no callback will be
  286. called! */
  287. }
  288. } else { /* skbs to send? */
  289. bcs->tx_skb = skb_dequeue(&bcs->squeue);
  290. if (bcs->tx_skb)
  291. gig_dbg(DEBUG_INTR,
  292. "Dequeued skb (Adr: %lx)!",
  293. (unsigned long) bcs->tx_skb);
  294. }
  295. }
  296. if (bcs->tx_skb) {
  297. gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
  298. if (write_modem(cs) < 0) {
  299. gig_dbg(DEBUG_OUTPUT,
  300. "modem_fill: write_modem failed");
  301. // FIXME should we tell the LL?
  302. again = 1; /* no callback will be called! */
  303. }
  304. }
  305. } while (again);
  306. }
  307. /**
  308. * gigaset_read_int_callback
  309. *
  310. * It is called if the data was received from the device.
  311. */
  312. static void gigaset_read_int_callback(struct urb *urb)
  313. {
  314. struct inbuf_t *inbuf = urb->context;
  315. struct cardstate *cs = inbuf->cs;
  316. int resubmit = 0;
  317. int r;
  318. unsigned numbytes;
  319. unsigned char *src;
  320. unsigned long flags;
  321. if (!urb->status) {
  322. if (!cs->connected) {
  323. err("%s: disconnected", __func__); /* should never happen */
  324. return;
  325. }
  326. numbytes = urb->actual_length;
  327. if (numbytes) {
  328. src = inbuf->rcvbuf;
  329. if (unlikely(*src))
  330. dev_warn(cs->dev,
  331. "%s: There was no leading 0, but 0x%02x!\n",
  332. __func__, (unsigned) *src);
  333. ++src; /* skip leading 0x00 */
  334. --numbytes;
  335. if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
  336. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  337. gigaset_schedule_event(inbuf->cs);
  338. }
  339. } else
  340. gig_dbg(DEBUG_INTR, "Received zero block length");
  341. resubmit = 1;
  342. } else {
  343. /* The urb might have been killed. */
  344. gig_dbg(DEBUG_ANY, "%s - nonzero read bulk status received: %d",
  345. __func__, urb->status);
  346. if (urb->status != -ENOENT) { /* not killed */
  347. if (!cs->connected) {
  348. err("%s: disconnected", __func__); /* should never happen */
  349. return;
  350. }
  351. resubmit = 1;
  352. }
  353. }
  354. if (resubmit) {
  355. spin_lock_irqsave(&cs->lock, flags);
  356. r = cs->connected ? usb_submit_urb(urb, SLAB_ATOMIC) : -ENODEV;
  357. spin_unlock_irqrestore(&cs->lock, flags);
  358. if (r)
  359. dev_err(cs->dev, "error %d when resubmitting urb.\n",
  360. -r);
  361. }
  362. }
  363. /* This callback routine is called when data was transmitted to the device. */
  364. static void gigaset_write_bulk_callback(struct urb *urb)
  365. {
  366. struct cardstate *cs = urb->context;
  367. unsigned long flags;
  368. if (urb->status)
  369. dev_err(cs->dev, "bulk transfer failed (status %d)\n",
  370. -urb->status);
  371. /* That's all we can do. Communication problems
  372. are handled by timeouts or network protocols. */
  373. spin_lock_irqsave(&cs->lock, flags);
  374. if (!cs->connected) {
  375. err("%s: not connected", __func__);
  376. } else {
  377. atomic_set(&cs->hw.usb->busy, 0);
  378. tasklet_schedule(&cs->write_tasklet);
  379. }
  380. spin_unlock_irqrestore(&cs->lock, flags);
  381. }
  382. static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
  383. {
  384. struct cmdbuf_t *tcb;
  385. unsigned long flags;
  386. int count;
  387. int status = -ENOENT; // FIXME
  388. struct usb_cardstate *ucs = cs->hw.usb;
  389. do {
  390. if (!cb->len) {
  391. tcb = cb;
  392. spin_lock_irqsave(&cs->cmdlock, flags);
  393. cs->cmdbytes -= cs->curlen;
  394. gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
  395. cs->curlen, cs->cmdbytes);
  396. cs->cmdbuf = cb = cb->next;
  397. if (cb) {
  398. cb->prev = NULL;
  399. cs->curlen = cb->len;
  400. } else {
  401. cs->lastcmdbuf = NULL;
  402. cs->curlen = 0;
  403. }
  404. spin_unlock_irqrestore(&cs->cmdlock, flags);
  405. if (tcb->wake_tasklet)
  406. tasklet_schedule(tcb->wake_tasklet);
  407. kfree(tcb);
  408. }
  409. if (cb) {
  410. count = min(cb->len, ucs->bulk_out_size);
  411. gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
  412. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  413. usb_sndbulkpipe(ucs->udev,
  414. ucs->bulk_out_endpointAddr & 0x0f),
  415. cb->buf + cb->offset, count,
  416. gigaset_write_bulk_callback, cs);
  417. cb->offset += count;
  418. cb->len -= count;
  419. atomic_set(&ucs->busy, 1);
  420. spin_lock_irqsave(&cs->lock, flags);
  421. status = cs->connected ? usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC) : -ENODEV;
  422. spin_unlock_irqrestore(&cs->lock, flags);
  423. if (status) {
  424. atomic_set(&ucs->busy, 0);
  425. err("could not submit urb (error %d)\n",
  426. -status);
  427. cb->len = 0; /* skip urb => remove cb+wakeup
  428. in next loop cycle */
  429. }
  430. }
  431. } while (cb && status); /* next command on error */
  432. return status;
  433. }
  434. /* Send command to device. */
  435. static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
  436. int len, struct tasklet_struct *wake_tasklet)
  437. {
  438. struct cmdbuf_t *cb;
  439. unsigned long flags;
  440. gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
  441. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  442. "CMD Transmit", len, buf);
  443. if (len <= 0)
  444. return 0;
  445. if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
  446. dev_err(cs->dev, "%s: out of memory\n", __func__);
  447. return -ENOMEM;
  448. }
  449. memcpy(cb->buf, buf, len);
  450. cb->len = len;
  451. cb->offset = 0;
  452. cb->next = NULL;
  453. cb->wake_tasklet = wake_tasklet;
  454. spin_lock_irqsave(&cs->cmdlock, flags);
  455. cb->prev = cs->lastcmdbuf;
  456. if (cs->lastcmdbuf)
  457. cs->lastcmdbuf->next = cb;
  458. else {
  459. cs->cmdbuf = cb;
  460. cs->curlen = len;
  461. }
  462. cs->cmdbytes += len;
  463. cs->lastcmdbuf = cb;
  464. spin_unlock_irqrestore(&cs->cmdlock, flags);
  465. spin_lock_irqsave(&cs->lock, flags);
  466. if (cs->connected)
  467. tasklet_schedule(&cs->write_tasklet);
  468. spin_unlock_irqrestore(&cs->lock, flags);
  469. return len;
  470. }
  471. static int gigaset_write_room(struct cardstate *cs)
  472. {
  473. unsigned long flags;
  474. unsigned bytes;
  475. spin_lock_irqsave(&cs->cmdlock, flags);
  476. bytes = cs->cmdbytes;
  477. spin_unlock_irqrestore(&cs->cmdlock, flags);
  478. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  479. }
  480. static int gigaset_chars_in_buffer(struct cardstate *cs)
  481. {
  482. return cs->cmdbytes;
  483. }
  484. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  485. {
  486. #ifdef CONFIG_GIGASET_UNDOCREQ
  487. struct usb_device *udev = cs->hw.usb->udev;
  488. gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
  489. memcpy(cs->hw.usb->bchars, buf, 6);
  490. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
  491. 0, 0, &buf, 6, 2000);
  492. #else
  493. return -EINVAL;
  494. #endif
  495. }
  496. static int gigaset_freebcshw(struct bc_state *bcs)
  497. {
  498. if (!bcs->hw.usb)
  499. return 0;
  500. //FIXME
  501. kfree(bcs->hw.usb);
  502. return 1;
  503. }
  504. /* Initialize the b-channel structure */
  505. static int gigaset_initbcshw(struct bc_state *bcs)
  506. {
  507. bcs->hw.usb = kmalloc(sizeof(struct usb_bc_state), GFP_KERNEL);
  508. if (!bcs->hw.usb)
  509. return 0;
  510. return 1;
  511. }
  512. static void gigaset_reinitbcshw(struct bc_state *bcs)
  513. {
  514. }
  515. static void gigaset_freecshw(struct cardstate *cs)
  516. {
  517. tasklet_kill(&cs->write_tasklet);
  518. kfree(cs->hw.usb);
  519. }
  520. static int gigaset_initcshw(struct cardstate *cs)
  521. {
  522. struct usb_cardstate *ucs;
  523. cs->hw.usb = ucs =
  524. kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
  525. if (!ucs)
  526. return 0;
  527. ucs->bchars[0] = 0;
  528. ucs->bchars[1] = 0;
  529. ucs->bchars[2] = 0;
  530. ucs->bchars[3] = 0;
  531. ucs->bchars[4] = 0x11;
  532. ucs->bchars[5] = 0x13;
  533. ucs->bulk_out_buffer = NULL;
  534. ucs->bulk_out_urb = NULL;
  535. //ucs->urb_cmd_out = NULL;
  536. ucs->read_urb = NULL;
  537. tasklet_init(&cs->write_tasklet,
  538. &gigaset_modem_fill, (unsigned long) cs);
  539. return 1;
  540. }
  541. /* Send data from current skb to the device. */
  542. static int write_modem(struct cardstate *cs)
  543. {
  544. int ret = 0;
  545. int count;
  546. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  547. struct usb_cardstate *ucs = cs->hw.usb;
  548. unsigned long flags;
  549. gig_dbg(DEBUG_WRITE, "len: %d...", bcs->tx_skb->len);
  550. if (!bcs->tx_skb->len) {
  551. dev_kfree_skb_any(bcs->tx_skb);
  552. bcs->tx_skb = NULL;
  553. return -EINVAL;
  554. }
  555. /* Copy data to bulk out buffer and // FIXME copying not necessary
  556. * transmit data
  557. */
  558. count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
  559. memcpy(ucs->bulk_out_buffer, bcs->tx_skb->data, count);
  560. skb_pull(bcs->tx_skb, count);
  561. atomic_set(&ucs->busy, 1);
  562. gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
  563. spin_lock_irqsave(&cs->lock, flags);
  564. if (cs->connected) {
  565. usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
  566. usb_sndbulkpipe(ucs->udev,
  567. ucs->bulk_out_endpointAddr & 0x0f),
  568. ucs->bulk_out_buffer, count,
  569. gigaset_write_bulk_callback, cs);
  570. ret = usb_submit_urb(ucs->bulk_out_urb, SLAB_ATOMIC);
  571. } else {
  572. ret = -ENODEV;
  573. }
  574. spin_unlock_irqrestore(&cs->lock, flags);
  575. if (ret) {
  576. err("could not submit urb (error %d)\n", -ret);
  577. atomic_set(&ucs->busy, 0);
  578. }
  579. if (!bcs->tx_skb->len) {
  580. /* skb sent completely */
  581. gigaset_skb_sent(bcs, bcs->tx_skb); //FIXME also, when ret<0?
  582. gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
  583. (unsigned long) bcs->tx_skb);
  584. dev_kfree_skb_any(bcs->tx_skb);
  585. bcs->tx_skb = NULL;
  586. }
  587. return ret;
  588. }
  589. static int gigaset_probe(struct usb_interface *interface,
  590. const struct usb_device_id *id)
  591. {
  592. int retval;
  593. struct usb_device *udev = interface_to_usbdev(interface);
  594. unsigned int ifnum;
  595. struct usb_host_interface *hostif;
  596. struct cardstate *cs = NULL;
  597. struct usb_cardstate *ucs = NULL;
  598. struct usb_endpoint_descriptor *endpoint;
  599. int buffer_size;
  600. int alt;
  601. gig_dbg(DEBUG_ANY,
  602. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  603. __func__, le16_to_cpu(udev->descriptor.idVendor),
  604. le16_to_cpu(udev->descriptor.idProduct));
  605. retval = -ENODEV; //FIXME
  606. /* See if the device offered us matches what we can accept */
  607. if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) ||
  608. (le16_to_cpu(udev->descriptor.idProduct) != USB_M105_PRODUCT_ID))
  609. return -ENODEV;
  610. /* this starts to become ascii art... */
  611. hostif = interface->cur_altsetting;
  612. alt = hostif->desc.bAlternateSetting;
  613. ifnum = hostif->desc.bInterfaceNumber; // FIXME ?
  614. if (alt != 0 || ifnum != 0) {
  615. dev_warn(&udev->dev, "ifnum %d, alt %d\n", ifnum, alt);
  616. return -ENODEV;
  617. }
  618. /* Reject application specific intefaces
  619. *
  620. */
  621. if (hostif->desc.bInterfaceClass != 255) {
  622. dev_info(&udev->dev,
  623. "%s: Device matched but iface_desc[%d]->bInterfaceClass==%d!\n",
  624. __func__, ifnum, hostif->desc.bInterfaceClass);
  625. return -ENODEV;
  626. }
  627. dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
  628. cs = gigaset_getunassignedcs(driver);
  629. if (!cs) {
  630. dev_warn(&udev->dev, "no free cardstate\n");
  631. return -ENODEV;
  632. }
  633. ucs = cs->hw.usb;
  634. /* save off device structure ptrs for later use */
  635. usb_get_dev(udev);
  636. ucs->udev = udev;
  637. ucs->interface = interface;
  638. cs->dev = &interface->dev;
  639. /* save address of controller structure */
  640. usb_set_intfdata(interface, cs); // dev_set_drvdata(&interface->dev, cs);
  641. endpoint = &hostif->endpoint[0].desc;
  642. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  643. ucs->bulk_out_size = buffer_size;
  644. ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  645. ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
  646. if (!ucs->bulk_out_buffer) {
  647. dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
  648. retval = -ENOMEM;
  649. goto error;
  650. }
  651. ucs->bulk_out_urb = usb_alloc_urb(0, SLAB_KERNEL);
  652. if (!ucs->bulk_out_urb) {
  653. dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
  654. retval = -ENOMEM;
  655. goto error;
  656. }
  657. endpoint = &hostif->endpoint[1].desc;
  658. atomic_set(&ucs->busy, 0);
  659. ucs->read_urb = usb_alloc_urb(0, SLAB_KERNEL);
  660. if (!ucs->read_urb) {
  661. dev_err(cs->dev, "No free urbs available\n");
  662. retval = -ENOMEM;
  663. goto error;
  664. }
  665. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  666. ucs->rcvbuf_size = buffer_size;
  667. ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
  668. cs->inbuf[0].rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
  669. if (!cs->inbuf[0].rcvbuf) {
  670. dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
  671. retval = -ENOMEM;
  672. goto error;
  673. }
  674. /* Fill the interrupt urb and send it to the core */
  675. usb_fill_int_urb(ucs->read_urb, udev,
  676. usb_rcvintpipe(udev,
  677. endpoint->bEndpointAddress & 0x0f),
  678. cs->inbuf[0].rcvbuf, buffer_size,
  679. gigaset_read_int_callback,
  680. cs->inbuf + 0, endpoint->bInterval);
  681. retval = usb_submit_urb(ucs->read_urb, SLAB_KERNEL);
  682. if (retval) {
  683. dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
  684. goto error;
  685. }
  686. /* tell common part that the device is ready */
  687. if (startmode == SM_LOCKED)
  688. atomic_set(&cs->mstate, MS_LOCKED);
  689. if (!gigaset_start(cs)) {
  690. tasklet_kill(&cs->write_tasklet);
  691. retval = -ENODEV; //FIXME
  692. goto error;
  693. }
  694. return 0;
  695. error:
  696. if (ucs->read_urb)
  697. usb_kill_urb(ucs->read_urb);
  698. kfree(ucs->bulk_out_buffer);
  699. if (ucs->bulk_out_urb != NULL)
  700. usb_free_urb(ucs->bulk_out_urb);
  701. kfree(cs->inbuf[0].rcvbuf);
  702. if (ucs->read_urb != NULL)
  703. usb_free_urb(ucs->read_urb);
  704. usb_set_intfdata(interface, NULL);
  705. ucs->read_urb = ucs->bulk_out_urb = NULL;
  706. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  707. usb_put_dev(ucs->udev);
  708. ucs->udev = NULL;
  709. ucs->interface = NULL;
  710. gigaset_unassign(cs);
  711. return retval;
  712. }
  713. static void gigaset_disconnect(struct usb_interface *interface)
  714. {
  715. struct cardstate *cs;
  716. struct usb_cardstate *ucs;
  717. cs = usb_get_intfdata(interface);
  718. ucs = cs->hw.usb;
  719. usb_kill_urb(ucs->read_urb);
  720. gigaset_stop(cs);
  721. usb_set_intfdata(interface, NULL);
  722. tasklet_kill(&cs->write_tasklet);
  723. usb_kill_urb(ucs->bulk_out_urb); /* FIXME: only if active? */
  724. kfree(ucs->bulk_out_buffer);
  725. if (ucs->bulk_out_urb != NULL)
  726. usb_free_urb(ucs->bulk_out_urb);
  727. kfree(cs->inbuf[0].rcvbuf);
  728. if (ucs->read_urb != NULL)
  729. usb_free_urb(ucs->read_urb);
  730. ucs->read_urb = ucs->bulk_out_urb = NULL;
  731. cs->inbuf[0].rcvbuf = ucs->bulk_out_buffer = NULL;
  732. usb_put_dev(ucs->udev);
  733. ucs->interface = NULL;
  734. ucs->udev = NULL;
  735. cs->dev = NULL;
  736. gigaset_unassign(cs);
  737. }
  738. static struct gigaset_ops ops = {
  739. gigaset_write_cmd,
  740. gigaset_write_room,
  741. gigaset_chars_in_buffer,
  742. gigaset_brkchars,
  743. gigaset_init_bchannel,
  744. gigaset_close_bchannel,
  745. gigaset_initbcshw,
  746. gigaset_freebcshw,
  747. gigaset_reinitbcshw,
  748. gigaset_initcshw,
  749. gigaset_freecshw,
  750. gigaset_set_modem_ctrl,
  751. gigaset_baud_rate,
  752. gigaset_set_line_ctrl,
  753. gigaset_m10x_send_skb,
  754. gigaset_m10x_input,
  755. };
  756. /**
  757. * usb_gigaset_init
  758. * This function is called while kernel-module is loaded
  759. */
  760. static int __init usb_gigaset_init(void)
  761. {
  762. int result;
  763. /* allocate memory for our driver state and intialize it */
  764. if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  765. GIGASET_MODULENAME, GIGASET_DEVNAME,
  766. &ops, THIS_MODULE)) == NULL)
  767. goto error;
  768. /* allocate memory for our device state and intialize it */
  769. cardstate = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  770. if (!cardstate)
  771. goto error;
  772. /* register this driver with the USB subsystem */
  773. result = usb_register(&gigaset_usb_driver);
  774. if (result < 0) {
  775. err("usb_gigaset: usb_register failed (error %d)",
  776. -result);
  777. goto error;
  778. }
  779. info(DRIVER_AUTHOR);
  780. info(DRIVER_DESC);
  781. return 0;
  782. error: if (cardstate)
  783. gigaset_freecs(cardstate);
  784. cardstate = NULL;
  785. if (driver)
  786. gigaset_freedriver(driver);
  787. driver = NULL;
  788. return -1;
  789. }
  790. /**
  791. * usb_gigaset_exit
  792. * This function is called while unloading the kernel-module
  793. */
  794. static void __exit usb_gigaset_exit(void)
  795. {
  796. gigaset_blockdriver(driver); /* => probe will fail
  797. * => no gigaset_start any more
  798. */
  799. gigaset_shutdown(cardstate);
  800. /* from now on, no isdn callback should be possible */
  801. /* deregister this driver with the USB subsystem */
  802. usb_deregister(&gigaset_usb_driver);
  803. /* this will call the disconnect-callback */
  804. /* from now on, no disconnect/probe callback should be running */
  805. gigaset_freecs(cardstate);
  806. cardstate = NULL;
  807. gigaset_freedriver(driver);
  808. driver = NULL;
  809. }
  810. module_init(usb_gigaset_init);
  811. module_exit(usb_gigaset_exit);
  812. MODULE_AUTHOR(DRIVER_AUTHOR);
  813. MODULE_DESCRIPTION(DRIVER_DESC);
  814. MODULE_LICENSE("GPL");