usb-gigaset.c 25 KB

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