cdc-acm.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
  6. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  10. *
  11. * USB Abstract Control Model driver for USB modems and ISDN adapters
  12. *
  13. * Sponsored by SuSE
  14. *
  15. * ChangeLog:
  16. * v0.9 - thorough cleaning, URBification, almost a rewrite
  17. * v0.10 - some more cleanups
  18. * v0.11 - fixed flow control, read error doesn't stop reads
  19. * v0.12 - added TIOCM ioctls, added break handling, made struct acm
  20. * kmalloced
  21. * v0.13 - added termios, added hangup
  22. * v0.14 - sized down struct acm
  23. * v0.15 - fixed flow control again - characters could be lost
  24. * v0.16 - added code for modems with swapped data and control interfaces
  25. * v0.17 - added new style probing
  26. * v0.18 - fixed new style probing for devices with more configurations
  27. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  28. * v0.20 - switched to probing on interface (rather than device) class
  29. * v0.21 - revert to probing on device for devices with multiple configs
  30. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  31. * config we want, sysadmin changes bConfigurationValue in sysfs.
  32. * v0.23 - use softirq for rx processing, as needed by tty layer
  33. * v0.24 - change probe method to evaluate CDC union descriptor
  34. * v0.25 - downstream tasks paralelized to maximize throughput
  35. * v0.26 - multiple write urbs, writesize increased
  36. */
  37. /*
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation; either version 2 of the License, or
  41. * (at your option) any later version.
  42. *
  43. * This program is distributed in the hope that it will be useful,
  44. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. * GNU General Public License for more details.
  47. *
  48. * You should have received a copy of the GNU General Public License
  49. * along with this program; if not, write to the Free Software
  50. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  51. */
  52. #undef DEBUG
  53. #undef VERBOSE_DEBUG
  54. #include <linux/kernel.h>
  55. #include <linux/errno.h>
  56. #include <linux/init.h>
  57. #include <linux/slab.h>
  58. #include <linux/tty.h>
  59. #include <linux/serial.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/module.h>
  63. #include <linux/mutex.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/usb.h>
  66. #include <linux/usb/cdc.h>
  67. #include <asm/byteorder.h>
  68. #include <asm/unaligned.h>
  69. #include <linux/list.h>
  70. #include "cdc-acm.h"
  71. #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
  72. /*
  73. * Version Information
  74. */
  75. #define DRIVER_VERSION "v0.26"
  76. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  77. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  78. static struct usb_driver acm_driver;
  79. static struct tty_driver *acm_tty_driver;
  80. static struct acm *acm_table[ACM_TTY_MINORS];
  81. static DEFINE_MUTEX(open_mutex);
  82. #define ACM_READY(acm) (acm && acm->dev && acm->port.count)
  83. static const struct tty_port_operations acm_port_ops = {
  84. };
  85. #ifdef VERBOSE_DEBUG
  86. #define verbose 1
  87. #else
  88. #define verbose 0
  89. #endif
  90. /*
  91. * Functions for ACM control messages.
  92. */
  93. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  94. void *buf, int len)
  95. {
  96. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  97. request, USB_RT_ACM, value,
  98. acm->control->altsetting[0].desc.bInterfaceNumber,
  99. buf, len, 5000);
  100. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d",
  101. request, value, len, retval);
  102. return retval < 0 ? retval : 0;
  103. }
  104. /* devices aren't required to support these requests.
  105. * the cdc acm descriptor tells whether they do...
  106. */
  107. #define acm_set_control(acm, control) \
  108. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  109. #define acm_set_line(acm, line) \
  110. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  111. #define acm_send_break(acm, ms) \
  112. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  113. /*
  114. * Write buffer management.
  115. * All of these assume proper locks taken by the caller.
  116. */
  117. static int acm_wb_alloc(struct acm *acm)
  118. {
  119. int i, wbn;
  120. struct acm_wb *wb;
  121. wbn = 0;
  122. i = 0;
  123. for (;;) {
  124. wb = &acm->wb[wbn];
  125. if (!wb->use) {
  126. wb->use = 1;
  127. return wbn;
  128. }
  129. wbn = (wbn + 1) % ACM_NW;
  130. if (++i >= ACM_NW)
  131. return -1;
  132. }
  133. }
  134. static int acm_wb_is_avail(struct acm *acm)
  135. {
  136. int i, n;
  137. unsigned long flags;
  138. n = ACM_NW;
  139. spin_lock_irqsave(&acm->write_lock, flags);
  140. for (i = 0; i < ACM_NW; i++)
  141. n -= acm->wb[i].use;
  142. spin_unlock_irqrestore(&acm->write_lock, flags);
  143. return n;
  144. }
  145. /*
  146. * Finish write. Caller must hold acm->write_lock
  147. */
  148. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  149. {
  150. wb->use = 0;
  151. acm->transmitting--;
  152. }
  153. /*
  154. * Poke write.
  155. *
  156. * the caller is responsible for locking
  157. */
  158. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  159. {
  160. int rc;
  161. acm->transmitting++;
  162. wb->urb->transfer_buffer = wb->buf;
  163. wb->urb->transfer_dma = wb->dmah;
  164. wb->urb->transfer_buffer_length = wb->len;
  165. wb->urb->dev = acm->dev;
  166. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  167. if (rc < 0) {
  168. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  169. acm_write_done(acm, wb);
  170. }
  171. return rc;
  172. }
  173. static int acm_write_start(struct acm *acm, int wbn)
  174. {
  175. unsigned long flags;
  176. struct acm_wb *wb = &acm->wb[wbn];
  177. int rc;
  178. spin_lock_irqsave(&acm->write_lock, flags);
  179. if (!acm->dev) {
  180. wb->use = 0;
  181. spin_unlock_irqrestore(&acm->write_lock, flags);
  182. return -ENODEV;
  183. }
  184. dbg("%s susp_count: %d", __func__, acm->susp_count);
  185. if (acm->susp_count) {
  186. acm->delayed_wb = wb;
  187. schedule_work(&acm->waker);
  188. spin_unlock_irqrestore(&acm->write_lock, flags);
  189. return 0; /* A white lie */
  190. }
  191. usb_mark_last_busy(acm->dev);
  192. rc = acm_start_wb(acm, wb);
  193. spin_unlock_irqrestore(&acm->write_lock, flags);
  194. return rc;
  195. }
  196. /*
  197. * attributes exported through sysfs
  198. */
  199. static ssize_t show_caps
  200. (struct device *dev, struct device_attribute *attr, char *buf)
  201. {
  202. struct usb_interface *intf = to_usb_interface(dev);
  203. struct acm *acm = usb_get_intfdata(intf);
  204. return sprintf(buf, "%d", acm->ctrl_caps);
  205. }
  206. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  207. static ssize_t show_country_codes
  208. (struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct usb_interface *intf = to_usb_interface(dev);
  211. struct acm *acm = usb_get_intfdata(intf);
  212. memcpy(buf, acm->country_codes, acm->country_code_size);
  213. return acm->country_code_size;
  214. }
  215. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  216. static ssize_t show_country_rel_date
  217. (struct device *dev, struct device_attribute *attr, char *buf)
  218. {
  219. struct usb_interface *intf = to_usb_interface(dev);
  220. struct acm *acm = usb_get_intfdata(intf);
  221. return sprintf(buf, "%d", acm->country_rel_date);
  222. }
  223. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  224. /*
  225. * Interrupt handlers for various ACM device responses
  226. */
  227. /* control interface reports status changes with "interrupt" transfers */
  228. static void acm_ctrl_irq(struct urb *urb)
  229. {
  230. struct acm *acm = urb->context;
  231. struct usb_cdc_notification *dr = urb->transfer_buffer;
  232. struct tty_struct *tty;
  233. unsigned char *data;
  234. int newctrl;
  235. int retval;
  236. int status = urb->status;
  237. switch (status) {
  238. case 0:
  239. /* success */
  240. break;
  241. case -ECONNRESET:
  242. case -ENOENT:
  243. case -ESHUTDOWN:
  244. /* this urb is terminated, clean up */
  245. dbg("%s - urb shutting down with status: %d", __func__, status);
  246. return;
  247. default:
  248. dbg("%s - nonzero urb status received: %d", __func__, status);
  249. goto exit;
  250. }
  251. if (!ACM_READY(acm))
  252. goto exit;
  253. data = (unsigned char *)(dr + 1);
  254. switch (dr->bNotificationType) {
  255. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  256. dbg("%s network", dr->wValue ?
  257. "connected to" : "disconnected from");
  258. break;
  259. case USB_CDC_NOTIFY_SERIAL_STATE:
  260. tty = tty_port_tty_get(&acm->port);
  261. newctrl = get_unaligned_le16(data);
  262. if (tty) {
  263. if (!acm->clocal &&
  264. (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  265. dbg("calling hangup");
  266. tty_hangup(tty);
  267. }
  268. tty_kref_put(tty);
  269. }
  270. acm->ctrlin = newctrl;
  271. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  272. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  273. acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  274. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  275. acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  276. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  277. acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  278. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  279. break;
  280. default:
  281. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  282. dr->bNotificationType, dr->wIndex,
  283. dr->wLength, data[0], data[1]);
  284. break;
  285. }
  286. exit:
  287. usb_mark_last_busy(acm->dev);
  288. retval = usb_submit_urb(urb, GFP_ATOMIC);
  289. if (retval)
  290. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
  291. "result %d", __func__, retval);
  292. }
  293. /* data interface returns incoming bytes, or we got unthrottled */
  294. static void acm_read_bulk(struct urb *urb)
  295. {
  296. struct acm_rb *buf;
  297. struct acm_ru *rcv = urb->context;
  298. struct acm *acm = rcv->instance;
  299. int status = urb->status;
  300. dbg("Entering acm_read_bulk with status %d", status);
  301. if (!ACM_READY(acm)) {
  302. dev_dbg(&acm->data->dev, "Aborting, acm not ready");
  303. return;
  304. }
  305. usb_mark_last_busy(acm->dev);
  306. if (status)
  307. dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
  308. buf = rcv->buffer;
  309. buf->size = urb->actual_length;
  310. if (likely(status == 0)) {
  311. spin_lock(&acm->read_lock);
  312. acm->processing++;
  313. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  314. list_add_tail(&buf->list, &acm->filled_read_bufs);
  315. spin_unlock(&acm->read_lock);
  316. } else {
  317. /* we drop the buffer due to an error */
  318. spin_lock(&acm->read_lock);
  319. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  320. list_add(&buf->list, &acm->spare_read_bufs);
  321. spin_unlock(&acm->read_lock);
  322. /* nevertheless the tasklet must be kicked unconditionally
  323. so the queue cannot dry up */
  324. }
  325. if (likely(!acm->susp_count))
  326. tasklet_schedule(&acm->urb_task);
  327. }
  328. static void acm_rx_tasklet(unsigned long _acm)
  329. {
  330. struct acm *acm = (void *)_acm;
  331. struct acm_rb *buf;
  332. struct tty_struct *tty;
  333. struct acm_ru *rcv;
  334. unsigned long flags;
  335. unsigned char throttled;
  336. dbg("Entering acm_rx_tasklet");
  337. if (!ACM_READY(acm)) {
  338. dbg("acm_rx_tasklet: ACM not ready");
  339. return;
  340. }
  341. spin_lock_irqsave(&acm->throttle_lock, flags);
  342. throttled = acm->throttle;
  343. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  344. if (throttled) {
  345. dbg("acm_rx_tasklet: throttled");
  346. return;
  347. }
  348. tty = tty_port_tty_get(&acm->port);
  349. next_buffer:
  350. spin_lock_irqsave(&acm->read_lock, flags);
  351. if (list_empty(&acm->filled_read_bufs)) {
  352. spin_unlock_irqrestore(&acm->read_lock, flags);
  353. goto urbs;
  354. }
  355. buf = list_entry(acm->filled_read_bufs.next,
  356. struct acm_rb, list);
  357. list_del(&buf->list);
  358. spin_unlock_irqrestore(&acm->read_lock, flags);
  359. dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
  360. if (tty) {
  361. spin_lock_irqsave(&acm->throttle_lock, flags);
  362. throttled = acm->throttle;
  363. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  364. if (!throttled) {
  365. tty_buffer_request_room(tty, buf->size);
  366. tty_insert_flip_string(tty, buf->base, buf->size);
  367. tty_flip_buffer_push(tty);
  368. } else {
  369. tty_kref_put(tty);
  370. dbg("Throttling noticed");
  371. spin_lock_irqsave(&acm->read_lock, flags);
  372. list_add(&buf->list, &acm->filled_read_bufs);
  373. spin_unlock_irqrestore(&acm->read_lock, flags);
  374. return;
  375. }
  376. }
  377. spin_lock_irqsave(&acm->read_lock, flags);
  378. list_add(&buf->list, &acm->spare_read_bufs);
  379. spin_unlock_irqrestore(&acm->read_lock, flags);
  380. goto next_buffer;
  381. urbs:
  382. tty_kref_put(tty);
  383. while (!list_empty(&acm->spare_read_bufs)) {
  384. spin_lock_irqsave(&acm->read_lock, flags);
  385. if (list_empty(&acm->spare_read_urbs)) {
  386. acm->processing = 0;
  387. spin_unlock_irqrestore(&acm->read_lock, flags);
  388. return;
  389. }
  390. rcv = list_entry(acm->spare_read_urbs.next,
  391. struct acm_ru, list);
  392. list_del(&rcv->list);
  393. spin_unlock_irqrestore(&acm->read_lock, flags);
  394. buf = list_entry(acm->spare_read_bufs.next,
  395. struct acm_rb, list);
  396. list_del(&buf->list);
  397. rcv->buffer = buf;
  398. if (acm->is_int_ep)
  399. usb_fill_int_urb(rcv->urb, acm->dev,
  400. acm->rx_endpoint,
  401. buf->base,
  402. acm->readsize,
  403. acm_read_bulk, rcv, acm->bInterval);
  404. else
  405. usb_fill_bulk_urb(rcv->urb, acm->dev,
  406. acm->rx_endpoint,
  407. buf->base,
  408. acm->readsize,
  409. acm_read_bulk, rcv);
  410. rcv->urb->transfer_dma = buf->dma;
  411. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  412. /* This shouldn't kill the driver as unsuccessful URBs are
  413. returned to the free-urbs-pool and resubmited ASAP */
  414. spin_lock_irqsave(&acm->read_lock, flags);
  415. if (acm->susp_count ||
  416. usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  417. list_add(&buf->list, &acm->spare_read_bufs);
  418. list_add(&rcv->list, &acm->spare_read_urbs);
  419. acm->processing = 0;
  420. spin_unlock_irqrestore(&acm->read_lock, flags);
  421. return;
  422. } else {
  423. spin_unlock_irqrestore(&acm->read_lock, flags);
  424. dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
  425. }
  426. }
  427. spin_lock_irqsave(&acm->read_lock, flags);
  428. acm->processing = 0;
  429. spin_unlock_irqrestore(&acm->read_lock, flags);
  430. }
  431. /* data interface wrote those outgoing bytes */
  432. static void acm_write_bulk(struct urb *urb)
  433. {
  434. struct acm_wb *wb = urb->context;
  435. struct acm *acm = wb->instance;
  436. unsigned long flags;
  437. if (verbose || urb->status
  438. || (urb->actual_length != urb->transfer_buffer_length))
  439. dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
  440. urb->actual_length,
  441. urb->transfer_buffer_length,
  442. urb->status);
  443. spin_lock_irqsave(&acm->write_lock, flags);
  444. acm_write_done(acm, wb);
  445. spin_unlock_irqrestore(&acm->write_lock, flags);
  446. if (ACM_READY(acm))
  447. schedule_work(&acm->work);
  448. else
  449. wake_up_interruptible(&acm->drain_wait);
  450. }
  451. static void acm_softint(struct work_struct *work)
  452. {
  453. struct acm *acm = container_of(work, struct acm, work);
  454. struct tty_struct *tty;
  455. dev_vdbg(&acm->data->dev, "tx work\n");
  456. if (!ACM_READY(acm))
  457. return;
  458. tty = tty_port_tty_get(&acm->port);
  459. tty_wakeup(tty);
  460. tty_kref_put(tty);
  461. }
  462. static void acm_waker(struct work_struct *waker)
  463. {
  464. struct acm *acm = container_of(waker, struct acm, waker);
  465. int rv;
  466. rv = usb_autopm_get_interface(acm->control);
  467. if (rv < 0) {
  468. dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
  469. return;
  470. }
  471. if (acm->delayed_wb) {
  472. acm_start_wb(acm, acm->delayed_wb);
  473. acm->delayed_wb = NULL;
  474. }
  475. usb_autopm_put_interface(acm->control);
  476. }
  477. /*
  478. * TTY handlers
  479. */
  480. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  481. {
  482. struct acm *acm;
  483. int rv = -ENODEV;
  484. int i;
  485. dbg("Entering acm_tty_open.");
  486. mutex_lock(&open_mutex);
  487. acm = acm_table[tty->index];
  488. if (!acm || !acm->dev)
  489. goto err_out;
  490. else
  491. rv = 0;
  492. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  493. tty->driver_data = acm;
  494. tty_port_tty_set(&acm->port, tty);
  495. if (usb_autopm_get_interface(acm->control) < 0)
  496. goto early_bail;
  497. else
  498. acm->control->needs_remote_wakeup = 1;
  499. mutex_lock(&acm->mutex);
  500. if (acm->port.count++) {
  501. usb_autopm_put_interface(acm->control);
  502. goto done;
  503. }
  504. acm->ctrlurb->dev = acm->dev;
  505. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  506. dbg("usb_submit_urb(ctrl irq) failed");
  507. goto bail_out;
  508. }
  509. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  510. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  511. goto full_bailout;
  512. usb_autopm_put_interface(acm->control);
  513. INIT_LIST_HEAD(&acm->spare_read_urbs);
  514. INIT_LIST_HEAD(&acm->spare_read_bufs);
  515. INIT_LIST_HEAD(&acm->filled_read_bufs);
  516. for (i = 0; i < acm->rx_buflimit; i++)
  517. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  518. for (i = 0; i < acm->rx_buflimit; i++)
  519. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  520. acm->throttle = 0;
  521. set_bit(ASYNCB_INITIALIZED, &acm->port.flags);
  522. rv = tty_port_block_til_ready(&acm->port, tty, filp);
  523. tasklet_schedule(&acm->urb_task);
  524. done:
  525. mutex_unlock(&acm->mutex);
  526. err_out:
  527. mutex_unlock(&open_mutex);
  528. return rv;
  529. full_bailout:
  530. usb_kill_urb(acm->ctrlurb);
  531. bail_out:
  532. usb_autopm_put_interface(acm->control);
  533. acm->port.count--;
  534. mutex_unlock(&acm->mutex);
  535. early_bail:
  536. mutex_unlock(&open_mutex);
  537. tty_port_tty_set(&acm->port, NULL);
  538. return -EIO;
  539. }
  540. static void acm_tty_unregister(struct acm *acm)
  541. {
  542. int i, nr;
  543. nr = acm->rx_buflimit;
  544. tty_unregister_device(acm_tty_driver, acm->minor);
  545. usb_put_intf(acm->control);
  546. acm_table[acm->minor] = NULL;
  547. usb_free_urb(acm->ctrlurb);
  548. for (i = 0; i < ACM_NW; i++)
  549. usb_free_urb(acm->wb[i].urb);
  550. for (i = 0; i < nr; i++)
  551. usb_free_urb(acm->ru[i].urb);
  552. kfree(acm->country_codes);
  553. kfree(acm);
  554. }
  555. static int acm_tty_chars_in_buffer(struct tty_struct *tty);
  556. static void acm_port_down(struct acm *acm, int drain)
  557. {
  558. int i, nr = acm->rx_buflimit;
  559. mutex_lock(&open_mutex);
  560. if (acm->dev) {
  561. usb_autopm_get_interface(acm->control);
  562. acm_set_control(acm, acm->ctrlout = 0);
  563. /* try letting the last writes drain naturally */
  564. if (drain) {
  565. wait_event_interruptible_timeout(acm->drain_wait,
  566. (ACM_NW == acm_wb_is_avail(acm)) || !acm->dev,
  567. ACM_CLOSE_TIMEOUT * HZ);
  568. }
  569. usb_kill_urb(acm->ctrlurb);
  570. for (i = 0; i < ACM_NW; i++)
  571. usb_kill_urb(acm->wb[i].urb);
  572. for (i = 0; i < nr; i++)
  573. usb_kill_urb(acm->ru[i].urb);
  574. acm->control->needs_remote_wakeup = 0;
  575. usb_autopm_put_interface(acm->control);
  576. }
  577. mutex_unlock(&open_mutex);
  578. }
  579. static void acm_tty_hangup(struct tty_struct *tty)
  580. {
  581. struct acm *acm = tty->driver_data;
  582. tty_port_hangup(&acm->port);
  583. acm_port_down(acm, 0);
  584. }
  585. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  586. {
  587. struct acm *acm = tty->driver_data;
  588. /* Perform the closing process and see if we need to do the hardware
  589. shutdown */
  590. if (!acm)
  591. return;
  592. if (tty_port_close_start(&acm->port, tty, filp) == 0) {
  593. mutex_lock(&open_mutex);
  594. if (!acm->dev) {
  595. tty_port_tty_set(&acm->port, NULL);
  596. acm_tty_unregister(acm);
  597. tty->driver_data = NULL;
  598. }
  599. mutex_unlock(&open_mutex);
  600. return;
  601. }
  602. acm_port_down(acm, 0);
  603. tty_port_close_end(&acm->port, tty);
  604. tty_port_tty_set(&acm->port, NULL);
  605. }
  606. static int acm_tty_write(struct tty_struct *tty,
  607. const unsigned char *buf, int count)
  608. {
  609. struct acm *acm = tty->driver_data;
  610. int stat;
  611. unsigned long flags;
  612. int wbn;
  613. struct acm_wb *wb;
  614. dbg("Entering acm_tty_write to write %d bytes,", count);
  615. if (!ACM_READY(acm))
  616. return -EINVAL;
  617. if (!count)
  618. return 0;
  619. spin_lock_irqsave(&acm->write_lock, flags);
  620. wbn = acm_wb_alloc(acm);
  621. if (wbn < 0) {
  622. spin_unlock_irqrestore(&acm->write_lock, flags);
  623. return 0;
  624. }
  625. wb = &acm->wb[wbn];
  626. count = (count > acm->writesize) ? acm->writesize : count;
  627. dbg("Get %d bytes...", count);
  628. memcpy(wb->buf, buf, count);
  629. wb->len = count;
  630. spin_unlock_irqrestore(&acm->write_lock, flags);
  631. stat = acm_write_start(acm, wbn);
  632. if (stat < 0)
  633. return stat;
  634. return count;
  635. }
  636. static int acm_tty_write_room(struct tty_struct *tty)
  637. {
  638. struct acm *acm = tty->driver_data;
  639. if (!ACM_READY(acm))
  640. return -EINVAL;
  641. /*
  642. * Do not let the line discipline to know that we have a reserve,
  643. * or it might get too enthusiastic.
  644. */
  645. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  646. }
  647. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  648. {
  649. struct acm *acm = tty->driver_data;
  650. if (!ACM_READY(acm))
  651. return 0;
  652. /*
  653. * This is inaccurate (overcounts), but it works.
  654. */
  655. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  656. }
  657. static void acm_tty_throttle(struct tty_struct *tty)
  658. {
  659. struct acm *acm = tty->driver_data;
  660. if (!ACM_READY(acm))
  661. return;
  662. spin_lock_bh(&acm->throttle_lock);
  663. acm->throttle = 1;
  664. spin_unlock_bh(&acm->throttle_lock);
  665. }
  666. static void acm_tty_unthrottle(struct tty_struct *tty)
  667. {
  668. struct acm *acm = tty->driver_data;
  669. if (!ACM_READY(acm))
  670. return;
  671. spin_lock_bh(&acm->throttle_lock);
  672. acm->throttle = 0;
  673. spin_unlock_bh(&acm->throttle_lock);
  674. tasklet_schedule(&acm->urb_task);
  675. }
  676. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  677. {
  678. struct acm *acm = tty->driver_data;
  679. int retval;
  680. if (!ACM_READY(acm))
  681. return -EINVAL;
  682. retval = acm_send_break(acm, state ? 0xffff : 0);
  683. if (retval < 0)
  684. dbg("send break failed");
  685. return retval;
  686. }
  687. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  688. {
  689. struct acm *acm = tty->driver_data;
  690. if (!ACM_READY(acm))
  691. return -EINVAL;
  692. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  693. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  694. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  695. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  696. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  697. TIOCM_CTS;
  698. }
  699. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  700. unsigned int set, unsigned int clear)
  701. {
  702. struct acm *acm = tty->driver_data;
  703. unsigned int newctrl;
  704. if (!ACM_READY(acm))
  705. return -EINVAL;
  706. newctrl = acm->ctrlout;
  707. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  708. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  709. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  710. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  711. newctrl = (newctrl & ~clear) | set;
  712. if (acm->ctrlout == newctrl)
  713. return 0;
  714. return acm_set_control(acm, acm->ctrlout = newctrl);
  715. }
  716. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file,
  717. unsigned int cmd, unsigned long arg)
  718. {
  719. struct acm *acm = tty->driver_data;
  720. if (!ACM_READY(acm))
  721. return -EINVAL;
  722. return -ENOIOCTLCMD;
  723. }
  724. static const __u32 acm_tty_speed[] = {
  725. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  726. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  727. 57600, 115200, 230400, 460800, 500000, 576000,
  728. 921600, 1000000, 1152000, 1500000, 2000000,
  729. 2500000, 3000000, 3500000, 4000000
  730. };
  731. static const __u8 acm_tty_size[] = {
  732. 5, 6, 7, 8
  733. };
  734. static void acm_tty_set_termios(struct tty_struct *tty,
  735. struct ktermios *termios_old)
  736. {
  737. struct acm *acm = tty->driver_data;
  738. struct ktermios *termios = tty->termios;
  739. struct usb_cdc_line_coding newline;
  740. int newctrl = acm->ctrlout;
  741. if (!ACM_READY(acm))
  742. return;
  743. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  744. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  745. newline.bParityType = termios->c_cflag & PARENB ?
  746. (termios->c_cflag & PARODD ? 1 : 2) +
  747. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  748. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  749. /* FIXME: Needs to clear unsupported bits in the termios */
  750. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  751. if (!newline.dwDTERate) {
  752. newline.dwDTERate = acm->line.dwDTERate;
  753. newctrl &= ~ACM_CTRL_DTR;
  754. } else
  755. newctrl |= ACM_CTRL_DTR;
  756. if (newctrl != acm->ctrlout)
  757. acm_set_control(acm, acm->ctrlout = newctrl);
  758. if (memcmp(&acm->line, &newline, sizeof newline)) {
  759. memcpy(&acm->line, &newline, sizeof newline);
  760. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  761. newline.bCharFormat, newline.bParityType,
  762. newline.bDataBits);
  763. acm_set_line(acm, &acm->line);
  764. }
  765. }
  766. /*
  767. * USB probe and disconnect routines.
  768. */
  769. /* Little helpers: write/read buffers free */
  770. static void acm_write_buffers_free(struct acm *acm)
  771. {
  772. int i;
  773. struct acm_wb *wb;
  774. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  775. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  776. usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
  777. }
  778. static void acm_read_buffers_free(struct acm *acm)
  779. {
  780. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  781. int i, n = acm->rx_buflimit;
  782. for (i = 0; i < n; i++)
  783. usb_buffer_free(usb_dev, acm->readsize,
  784. acm->rb[i].base, acm->rb[i].dma);
  785. }
  786. /* Little helper: write buffers allocate */
  787. static int acm_write_buffers_alloc(struct acm *acm)
  788. {
  789. int i;
  790. struct acm_wb *wb;
  791. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  792. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  793. &wb->dmah);
  794. if (!wb->buf) {
  795. while (i != 0) {
  796. --i;
  797. --wb;
  798. usb_buffer_free(acm->dev, acm->writesize,
  799. wb->buf, wb->dmah);
  800. }
  801. return -ENOMEM;
  802. }
  803. }
  804. return 0;
  805. }
  806. static int acm_probe(struct usb_interface *intf,
  807. const struct usb_device_id *id)
  808. {
  809. struct usb_cdc_union_desc *union_header = NULL;
  810. struct usb_cdc_country_functional_desc *cfd = NULL;
  811. unsigned char *buffer = intf->altsetting->extra;
  812. int buflen = intf->altsetting->extralen;
  813. struct usb_interface *control_interface;
  814. struct usb_interface *data_interface;
  815. struct usb_endpoint_descriptor *epctrl = NULL;
  816. struct usb_endpoint_descriptor *epread = NULL;
  817. struct usb_endpoint_descriptor *epwrite = NULL;
  818. struct usb_device *usb_dev = interface_to_usbdev(intf);
  819. struct acm *acm;
  820. int minor;
  821. int ctrlsize, readsize;
  822. u8 *buf;
  823. u8 ac_management_function = 0;
  824. u8 call_management_function = 0;
  825. int call_interface_num = -1;
  826. int data_interface_num;
  827. unsigned long quirks;
  828. int num_rx_buf;
  829. int i;
  830. int combined_interfaces = 0;
  831. /* normal quirks */
  832. quirks = (unsigned long)id->driver_info;
  833. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  834. /* handle quirks deadly to normal probing*/
  835. if (quirks == NO_UNION_NORMAL) {
  836. data_interface = usb_ifnum_to_if(usb_dev, 1);
  837. control_interface = usb_ifnum_to_if(usb_dev, 0);
  838. goto skip_normal_probe;
  839. }
  840. /* normal probing*/
  841. if (!buffer) {
  842. dev_err(&intf->dev, "Weird descriptor references\n");
  843. return -EINVAL;
  844. }
  845. if (!buflen) {
  846. if (intf->cur_altsetting->endpoint->extralen &&
  847. intf->cur_altsetting->endpoint->extra) {
  848. dev_dbg(&intf->dev,
  849. "Seeking extra descriptors on endpoint\n");
  850. buflen = intf->cur_altsetting->endpoint->extralen;
  851. buffer = intf->cur_altsetting->endpoint->extra;
  852. } else {
  853. dev_err(&intf->dev,
  854. "Zero length descriptor references\n");
  855. return -EINVAL;
  856. }
  857. }
  858. while (buflen > 0) {
  859. if (buffer[1] != USB_DT_CS_INTERFACE) {
  860. dev_err(&intf->dev, "skipping garbage\n");
  861. goto next_desc;
  862. }
  863. switch (buffer[2]) {
  864. case USB_CDC_UNION_TYPE: /* we've found it */
  865. if (union_header) {
  866. dev_err(&intf->dev, "More than one "
  867. "union descriptor, skipping ...\n");
  868. goto next_desc;
  869. }
  870. union_header = (struct usb_cdc_union_desc *)buffer;
  871. break;
  872. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  873. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  874. break;
  875. case USB_CDC_HEADER_TYPE: /* maybe check version */
  876. break; /* for now we ignore it */
  877. case USB_CDC_ACM_TYPE:
  878. ac_management_function = buffer[3];
  879. break;
  880. case USB_CDC_CALL_MANAGEMENT_TYPE:
  881. call_management_function = buffer[3];
  882. call_interface_num = buffer[4];
  883. if ((call_management_function & 3) != 3)
  884. dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
  885. break;
  886. default:
  887. /* there are LOTS more CDC descriptors that
  888. * could legitimately be found here.
  889. */
  890. dev_dbg(&intf->dev, "Ignoring descriptor: "
  891. "type %02x, length %d\n",
  892. buffer[2], buffer[0]);
  893. break;
  894. }
  895. next_desc:
  896. buflen -= buffer[0];
  897. buffer += buffer[0];
  898. }
  899. if (!union_header) {
  900. if (call_interface_num > 0) {
  901. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  902. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  903. control_interface = intf;
  904. } else {
  905. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  906. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  907. return -ENODEV;
  908. } else {
  909. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  910. combined_interfaces = 1;
  911. control_interface = data_interface = intf;
  912. goto look_for_collapsed_interface;
  913. }
  914. }
  915. } else {
  916. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  917. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  918. if (!control_interface || !data_interface) {
  919. dev_dbg(&intf->dev, "no interfaces\n");
  920. return -ENODEV;
  921. }
  922. }
  923. if (data_interface_num != call_interface_num)
  924. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  925. if (control_interface == data_interface) {
  926. /* some broken devices designed for windows work this way */
  927. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  928. combined_interfaces = 1;
  929. /* a popular other OS doesn't use it */
  930. quirks |= NO_CAP_LINE;
  931. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  932. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  933. return -EINVAL;
  934. }
  935. look_for_collapsed_interface:
  936. for (i = 0; i < 3; i++) {
  937. struct usb_endpoint_descriptor *ep;
  938. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  939. if (usb_endpoint_is_int_in(ep))
  940. epctrl = ep;
  941. else if (usb_endpoint_is_bulk_out(ep))
  942. epwrite = ep;
  943. else if (usb_endpoint_is_bulk_in(ep))
  944. epread = ep;
  945. else
  946. return -EINVAL;
  947. }
  948. if (!epctrl || !epread || !epwrite)
  949. return -ENODEV;
  950. else
  951. goto made_compressed_probe;
  952. }
  953. skip_normal_probe:
  954. /*workaround for switched interfaces */
  955. if (data_interface->cur_altsetting->desc.bInterfaceClass
  956. != CDC_DATA_INTERFACE_TYPE) {
  957. if (control_interface->cur_altsetting->desc.bInterfaceClass
  958. == CDC_DATA_INTERFACE_TYPE) {
  959. struct usb_interface *t;
  960. dev_dbg(&intf->dev,
  961. "Your device has switched interfaces.\n");
  962. t = control_interface;
  963. control_interface = data_interface;
  964. data_interface = t;
  965. } else {
  966. return -EINVAL;
  967. }
  968. }
  969. /* Accept probe requests only for the control interface */
  970. if (!combined_interfaces && intf != control_interface)
  971. return -ENODEV;
  972. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  973. /* valid in this context */
  974. dev_dbg(&intf->dev, "The data interface isn't available\n");
  975. return -EBUSY;
  976. }
  977. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  978. return -EINVAL;
  979. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  980. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  981. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  982. /* workaround for switched endpoints */
  983. if (!usb_endpoint_dir_in(epread)) {
  984. /* descriptors are swapped */
  985. struct usb_endpoint_descriptor *t;
  986. dev_dbg(&intf->dev,
  987. "The data interface has switched endpoints\n");
  988. t = epread;
  989. epread = epwrite;
  990. epwrite = t;
  991. }
  992. made_compressed_probe:
  993. dbg("interfaces are valid");
  994. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  995. if (minor == ACM_TTY_MINORS) {
  996. dev_err(&intf->dev, "no more free acm devices\n");
  997. return -ENODEV;
  998. }
  999. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  1000. if (acm == NULL) {
  1001. dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
  1002. goto alloc_fail;
  1003. }
  1004. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  1005. readsize = le16_to_cpu(epread->wMaxPacketSize) *
  1006. (quirks == SINGLE_RX_URB ? 1 : 2);
  1007. acm->combined_interfaces = combined_interfaces;
  1008. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
  1009. acm->control = control_interface;
  1010. acm->data = data_interface;
  1011. acm->minor = minor;
  1012. acm->dev = usb_dev;
  1013. acm->ctrl_caps = ac_management_function;
  1014. if (quirks & NO_CAP_LINE)
  1015. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1016. acm->ctrlsize = ctrlsize;
  1017. acm->readsize = readsize;
  1018. acm->rx_buflimit = num_rx_buf;
  1019. acm->urb_task.func = acm_rx_tasklet;
  1020. acm->urb_task.data = (unsigned long) acm;
  1021. INIT_WORK(&acm->work, acm_softint);
  1022. INIT_WORK(&acm->waker, acm_waker);
  1023. init_waitqueue_head(&acm->drain_wait);
  1024. spin_lock_init(&acm->throttle_lock);
  1025. spin_lock_init(&acm->write_lock);
  1026. spin_lock_init(&acm->read_lock);
  1027. mutex_init(&acm->mutex);
  1028. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  1029. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  1030. if (acm->is_int_ep)
  1031. acm->bInterval = epread->bInterval;
  1032. tty_port_init(&acm->port);
  1033. acm->port.ops = &acm_port_ops;
  1034. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1035. if (!buf) {
  1036. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  1037. goto alloc_fail2;
  1038. }
  1039. acm->ctrl_buffer = buf;
  1040. if (acm_write_buffers_alloc(acm) < 0) {
  1041. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  1042. goto alloc_fail4;
  1043. }
  1044. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1045. if (!acm->ctrlurb) {
  1046. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  1047. goto alloc_fail5;
  1048. }
  1049. for (i = 0; i < num_rx_buf; i++) {
  1050. struct acm_ru *rcv = &(acm->ru[i]);
  1051. rcv->urb = usb_alloc_urb(0, GFP_KERNEL);
  1052. if (rcv->urb == NULL) {
  1053. dev_dbg(&intf->dev,
  1054. "out of memory (read urbs usb_alloc_urb)\n");
  1055. goto alloc_fail7;
  1056. }
  1057. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1058. rcv->instance = acm;
  1059. }
  1060. for (i = 0; i < num_rx_buf; i++) {
  1061. struct acm_rb *rb = &(acm->rb[i]);
  1062. rb->base = usb_buffer_alloc(acm->dev, readsize,
  1063. GFP_KERNEL, &rb->dma);
  1064. if (!rb->base) {
  1065. dev_dbg(&intf->dev,
  1066. "out of memory (read bufs usb_buffer_alloc)\n");
  1067. goto alloc_fail7;
  1068. }
  1069. }
  1070. for (i = 0; i < ACM_NW; i++) {
  1071. struct acm_wb *snd = &(acm->wb[i]);
  1072. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1073. if (snd->urb == NULL) {
  1074. dev_dbg(&intf->dev,
  1075. "out of memory (write urbs usb_alloc_urb)");
  1076. goto alloc_fail7;
  1077. }
  1078. if (usb_endpoint_xfer_int(epwrite))
  1079. usb_fill_int_urb(snd->urb, usb_dev,
  1080. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1081. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1082. else
  1083. usb_fill_bulk_urb(snd->urb, usb_dev,
  1084. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1085. NULL, acm->writesize, acm_write_bulk, snd);
  1086. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1087. snd->instance = acm;
  1088. }
  1089. usb_set_intfdata(intf, acm);
  1090. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1091. if (i < 0)
  1092. goto alloc_fail8;
  1093. if (cfd) { /* export the country data */
  1094. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1095. if (!acm->country_codes)
  1096. goto skip_countries;
  1097. acm->country_code_size = cfd->bLength - 4;
  1098. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1099. cfd->bLength - 4);
  1100. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1101. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1102. if (i < 0) {
  1103. kfree(acm->country_codes);
  1104. goto skip_countries;
  1105. }
  1106. i = device_create_file(&intf->dev,
  1107. &dev_attr_iCountryCodeRelDate);
  1108. if (i < 0) {
  1109. kfree(acm->country_codes);
  1110. goto skip_countries;
  1111. }
  1112. }
  1113. skip_countries:
  1114. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1115. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1116. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1117. /* works around buggy devices */
  1118. epctrl->bInterval ? epctrl->bInterval : 0xff);
  1119. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1120. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1121. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1122. acm_set_control(acm, acm->ctrlout);
  1123. acm->line.dwDTERate = cpu_to_le32(9600);
  1124. acm->line.bDataBits = 8;
  1125. acm_set_line(acm, &acm->line);
  1126. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1127. usb_set_intfdata(data_interface, acm);
  1128. usb_get_intf(control_interface);
  1129. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1130. acm_table[minor] = acm;
  1131. return 0;
  1132. alloc_fail8:
  1133. for (i = 0; i < ACM_NW; i++)
  1134. usb_free_urb(acm->wb[i].urb);
  1135. alloc_fail7:
  1136. acm_read_buffers_free(acm);
  1137. for (i = 0; i < num_rx_buf; i++)
  1138. usb_free_urb(acm->ru[i].urb);
  1139. usb_free_urb(acm->ctrlurb);
  1140. alloc_fail5:
  1141. acm_write_buffers_free(acm);
  1142. alloc_fail4:
  1143. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1144. alloc_fail2:
  1145. kfree(acm);
  1146. alloc_fail:
  1147. return -ENOMEM;
  1148. }
  1149. static void stop_data_traffic(struct acm *acm)
  1150. {
  1151. int i;
  1152. dbg("Entering stop_data_traffic");
  1153. tasklet_disable(&acm->urb_task);
  1154. usb_kill_urb(acm->ctrlurb);
  1155. for (i = 0; i < ACM_NW; i++)
  1156. usb_kill_urb(acm->wb[i].urb);
  1157. for (i = 0; i < acm->rx_buflimit; i++)
  1158. usb_kill_urb(acm->ru[i].urb);
  1159. tasklet_enable(&acm->urb_task);
  1160. cancel_work_sync(&acm->work);
  1161. cancel_work_sync(&acm->waker);
  1162. }
  1163. static void acm_disconnect(struct usb_interface *intf)
  1164. {
  1165. struct acm *acm = usb_get_intfdata(intf);
  1166. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1167. struct tty_struct *tty;
  1168. /* sibling interface is already cleaning up */
  1169. if (!acm)
  1170. return;
  1171. mutex_lock(&open_mutex);
  1172. if (acm->country_codes) {
  1173. device_remove_file(&acm->control->dev,
  1174. &dev_attr_wCountryCodes);
  1175. device_remove_file(&acm->control->dev,
  1176. &dev_attr_iCountryCodeRelDate);
  1177. }
  1178. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1179. acm->dev = NULL;
  1180. usb_set_intfdata(acm->control, NULL);
  1181. usb_set_intfdata(acm->data, NULL);
  1182. stop_data_traffic(acm);
  1183. acm_write_buffers_free(acm);
  1184. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer,
  1185. acm->ctrl_dma);
  1186. acm_read_buffers_free(acm);
  1187. if (!acm->combined_interfaces)
  1188. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1189. acm->data : acm->control);
  1190. if (acm->port.count == 0) {
  1191. acm_tty_unregister(acm);
  1192. mutex_unlock(&open_mutex);
  1193. return;
  1194. }
  1195. mutex_unlock(&open_mutex);
  1196. tty = tty_port_tty_get(&acm->port);
  1197. if (tty) {
  1198. tty_hangup(tty);
  1199. tty_kref_put(tty);
  1200. }
  1201. }
  1202. #ifdef CONFIG_PM
  1203. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1204. {
  1205. struct acm *acm = usb_get_intfdata(intf);
  1206. int cnt;
  1207. if (message.event & PM_EVENT_AUTO) {
  1208. int b;
  1209. spin_lock_irq(&acm->read_lock);
  1210. spin_lock(&acm->write_lock);
  1211. b = acm->processing + acm->transmitting;
  1212. spin_unlock(&acm->write_lock);
  1213. spin_unlock_irq(&acm->read_lock);
  1214. if (b)
  1215. return -EBUSY;
  1216. }
  1217. spin_lock_irq(&acm->read_lock);
  1218. spin_lock(&acm->write_lock);
  1219. cnt = acm->susp_count++;
  1220. spin_unlock(&acm->write_lock);
  1221. spin_unlock_irq(&acm->read_lock);
  1222. if (cnt)
  1223. return 0;
  1224. /*
  1225. we treat opened interfaces differently,
  1226. we must guard against open
  1227. */
  1228. mutex_lock(&acm->mutex);
  1229. if (acm->port.count)
  1230. stop_data_traffic(acm);
  1231. mutex_unlock(&acm->mutex);
  1232. return 0;
  1233. }
  1234. static int acm_resume(struct usb_interface *intf)
  1235. {
  1236. struct acm *acm = usb_get_intfdata(intf);
  1237. int rv = 0;
  1238. int cnt;
  1239. spin_lock_irq(&acm->read_lock);
  1240. acm->susp_count -= 1;
  1241. cnt = acm->susp_count;
  1242. spin_unlock_irq(&acm->read_lock);
  1243. if (cnt)
  1244. return 0;
  1245. mutex_lock(&acm->mutex);
  1246. if (acm->port.count) {
  1247. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1248. if (rv < 0)
  1249. goto err_out;
  1250. tasklet_schedule(&acm->urb_task);
  1251. }
  1252. err_out:
  1253. mutex_unlock(&acm->mutex);
  1254. return rv;
  1255. }
  1256. #endif /* CONFIG_PM */
  1257. /*
  1258. * USB driver structure.
  1259. */
  1260. static struct usb_device_id acm_ids[] = {
  1261. /* quirky and broken devices */
  1262. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1263. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1264. },
  1265. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1266. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1267. },
  1268. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1269. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1270. },
  1271. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1272. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1273. },
  1274. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1275. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1276. },
  1277. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1278. .driver_info = SINGLE_RX_URB,
  1279. },
  1280. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1281. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1282. },
  1283. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1284. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1285. },
  1286. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1287. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1288. },
  1289. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1290. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1291. },
  1292. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1293. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1294. },
  1295. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1296. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1297. },
  1298. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1299. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1300. },
  1301. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1302. },
  1303. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1304. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1305. data interface instead of
  1306. communications interface.
  1307. Maybe we should define a new
  1308. quirk for this. */
  1309. },
  1310. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1311. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1312. },
  1313. /* control interfaces with various AT-command sets */
  1314. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1315. USB_CDC_ACM_PROTO_AT_V25TER) },
  1316. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1317. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1318. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1319. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1320. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1321. USB_CDC_ACM_PROTO_AT_GSM) },
  1322. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1323. USB_CDC_ACM_PROTO_AT_3G) },
  1324. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1325. USB_CDC_ACM_PROTO_AT_CDMA) },
  1326. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  1327. { }
  1328. };
  1329. MODULE_DEVICE_TABLE(usb, acm_ids);
  1330. static struct usb_driver acm_driver = {
  1331. .name = "cdc_acm",
  1332. .probe = acm_probe,
  1333. .disconnect = acm_disconnect,
  1334. #ifdef CONFIG_PM
  1335. .suspend = acm_suspend,
  1336. .resume = acm_resume,
  1337. #endif
  1338. .id_table = acm_ids,
  1339. #ifdef CONFIG_PM
  1340. .supports_autosuspend = 1,
  1341. #endif
  1342. };
  1343. /*
  1344. * TTY driver structures.
  1345. */
  1346. static const struct tty_operations acm_ops = {
  1347. .open = acm_tty_open,
  1348. .close = acm_tty_close,
  1349. .hangup = acm_tty_hangup,
  1350. .write = acm_tty_write,
  1351. .write_room = acm_tty_write_room,
  1352. .ioctl = acm_tty_ioctl,
  1353. .throttle = acm_tty_throttle,
  1354. .unthrottle = acm_tty_unthrottle,
  1355. .chars_in_buffer = acm_tty_chars_in_buffer,
  1356. .break_ctl = acm_tty_break_ctl,
  1357. .set_termios = acm_tty_set_termios,
  1358. .tiocmget = acm_tty_tiocmget,
  1359. .tiocmset = acm_tty_tiocmset,
  1360. };
  1361. /*
  1362. * Init / exit.
  1363. */
  1364. static int __init acm_init(void)
  1365. {
  1366. int retval;
  1367. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1368. if (!acm_tty_driver)
  1369. return -ENOMEM;
  1370. acm_tty_driver->owner = THIS_MODULE,
  1371. acm_tty_driver->driver_name = "acm",
  1372. acm_tty_driver->name = "ttyACM",
  1373. acm_tty_driver->major = ACM_TTY_MAJOR,
  1374. acm_tty_driver->minor_start = 0,
  1375. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1376. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1377. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1378. acm_tty_driver->init_termios = tty_std_termios;
  1379. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1380. HUPCL | CLOCAL;
  1381. tty_set_operations(acm_tty_driver, &acm_ops);
  1382. retval = tty_register_driver(acm_tty_driver);
  1383. if (retval) {
  1384. put_tty_driver(acm_tty_driver);
  1385. return retval;
  1386. }
  1387. retval = usb_register(&acm_driver);
  1388. if (retval) {
  1389. tty_unregister_driver(acm_tty_driver);
  1390. put_tty_driver(acm_tty_driver);
  1391. return retval;
  1392. }
  1393. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1394. DRIVER_DESC "\n");
  1395. return 0;
  1396. }
  1397. static void __exit acm_exit(void)
  1398. {
  1399. usb_deregister(&acm_driver);
  1400. tty_unregister_driver(acm_tty_driver);
  1401. put_tty_driver(acm_tty_driver);
  1402. }
  1403. module_init(acm_init);
  1404. module_exit(acm_exit);
  1405. MODULE_AUTHOR(DRIVER_AUTHOR);
  1406. MODULE_DESCRIPTION(DRIVER_DESC);
  1407. MODULE_LICENSE("GPL");
  1408. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);