cdc-acm.c 47 KB

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