hso.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define DRIVER_VERSION "1.2"
  73. #define MOD_AUTHOR "Option Wireless"
  74. #define MOD_DESCRIPTION "USB High Speed Option driver"
  75. #define MOD_LICENSE "GPL"
  76. #define HSO_MAX_NET_DEVICES 10
  77. #define HSO__MAX_MTU 2048
  78. #define DEFAULT_MTU 1500
  79. #define DEFAULT_MRU 1500
  80. #define CTRL_URB_RX_SIZE 1024
  81. #define CTRL_URB_TX_SIZE 64
  82. #define BULK_URB_RX_SIZE 4096
  83. #define BULK_URB_TX_SIZE 8192
  84. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  86. #define MUX_BULK_RX_BUF_COUNT 4
  87. #define USB_TYPE_OPTION_VENDOR 0x20
  88. /* These definitions are used with the struct hso_net flags element */
  89. /* - use *_bit operations on it. (bit indices not values.) */
  90. #define HSO_NET_RUNNING 0
  91. #define HSO_NET_TX_TIMEOUT (HZ*10)
  92. #define HSO_SERIAL_MAGIC 0x48534f31
  93. /* Number of ttys to handle */
  94. #define HSO_SERIAL_TTY_MINORS 256
  95. #define MAX_RX_URBS 2
  96. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  97. {
  98. if (tty)
  99. return tty->driver_data;
  100. return NULL;
  101. }
  102. /*****************************************************************************/
  103. /* Debugging functions */
  104. /*****************************************************************************/
  105. #define D__(lvl_, fmt, arg...) \
  106. do { \
  107. printk(lvl_ "[%d:%s]: " fmt "\n", \
  108. __LINE__, __func__, ## arg); \
  109. } while (0)
  110. #define D_(lvl, args...) \
  111. do { \
  112. if (lvl & debug) \
  113. D__(KERN_INFO, args); \
  114. } while (0)
  115. #define D1(args...) D_(0x01, ##args)
  116. #define D2(args...) D_(0x02, ##args)
  117. #define D3(args...) D_(0x04, ##args)
  118. #define D4(args...) D_(0x08, ##args)
  119. #define D5(args...) D_(0x10, ##args)
  120. /*****************************************************************************/
  121. /* Enumerators */
  122. /*****************************************************************************/
  123. enum pkt_parse_state {
  124. WAIT_IP,
  125. WAIT_DATA,
  126. WAIT_SYNC
  127. };
  128. /*****************************************************************************/
  129. /* Structs */
  130. /*****************************************************************************/
  131. struct hso_shared_int {
  132. struct usb_endpoint_descriptor *intr_endp;
  133. void *shared_intr_buf;
  134. struct urb *shared_intr_urb;
  135. struct usb_device *usb;
  136. int use_count;
  137. int ref_count;
  138. struct mutex shared_int_lock;
  139. };
  140. struct hso_net {
  141. struct hso_device *parent;
  142. struct net_device *net;
  143. struct rfkill *rfkill;
  144. struct usb_endpoint_descriptor *in_endp;
  145. struct usb_endpoint_descriptor *out_endp;
  146. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  147. struct urb *mux_bulk_tx_urb;
  148. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  149. void *mux_bulk_tx_buf;
  150. struct sk_buff *skb_rx_buf;
  151. struct sk_buff *skb_tx_buf;
  152. enum pkt_parse_state rx_parse_state;
  153. spinlock_t net_lock;
  154. unsigned short rx_buf_size;
  155. unsigned short rx_buf_missing;
  156. struct iphdr rx_ip_hdr;
  157. unsigned long flags;
  158. };
  159. enum rx_ctrl_state{
  160. RX_IDLE,
  161. RX_SENT,
  162. RX_PENDING
  163. };
  164. #define BM_REQUEST_TYPE (0xa1)
  165. #define B_NOTIFICATION (0x20)
  166. #define W_VALUE (0x0)
  167. #define W_INDEX (0x2)
  168. #define W_LENGTH (0x2)
  169. #define B_OVERRUN (0x1<<6)
  170. #define B_PARITY (0x1<<5)
  171. #define B_FRAMING (0x1<<4)
  172. #define B_RING_SIGNAL (0x1<<3)
  173. #define B_BREAK (0x1<<2)
  174. #define B_TX_CARRIER (0x1<<1)
  175. #define B_RX_CARRIER (0x1<<0)
  176. struct hso_serial_state_notification {
  177. u8 bmRequestType;
  178. u8 bNotification;
  179. u16 wValue;
  180. u16 wIndex;
  181. u16 wLength;
  182. u16 UART_state_bitmap;
  183. } __attribute__((packed));
  184. struct hso_tiocmget {
  185. struct mutex mutex;
  186. wait_queue_head_t waitq;
  187. int intr_completed;
  188. struct usb_endpoint_descriptor *endp;
  189. struct urb *urb;
  190. struct hso_serial_state_notification serial_state_notification;
  191. u16 prev_UART_state_bitmap;
  192. struct uart_icount icount;
  193. };
  194. struct hso_serial {
  195. struct hso_device *parent;
  196. int magic;
  197. u8 minor;
  198. struct hso_shared_int *shared_int;
  199. /* rx/tx urb could be either a bulk urb or a control urb depending
  200. on which serial port it is used on. */
  201. struct urb *rx_urb[MAX_RX_URBS];
  202. u8 num_rx_urbs;
  203. u8 *rx_data[MAX_RX_URBS];
  204. u16 rx_data_length; /* should contain allocated length */
  205. struct urb *tx_urb;
  206. u8 *tx_data;
  207. u8 *tx_buffer;
  208. u16 tx_data_length; /* should contain allocated length */
  209. u16 tx_data_count;
  210. u16 tx_buffer_count;
  211. struct usb_ctrlrequest ctrl_req_tx;
  212. struct usb_ctrlrequest ctrl_req_rx;
  213. struct usb_endpoint_descriptor *in_endp;
  214. struct usb_endpoint_descriptor *out_endp;
  215. enum rx_ctrl_state rx_state;
  216. u8 rts_state;
  217. u8 dtr_state;
  218. unsigned tx_urb_used:1;
  219. /* from usb_serial_port */
  220. struct tty_struct *tty;
  221. int open_count;
  222. spinlock_t serial_lock;
  223. int (*write_data) (struct hso_serial *serial);
  224. struct hso_tiocmget *tiocmget;
  225. /* Hacks required to get flow control
  226. * working on the serial receive buffers
  227. * so as not to drop characters on the floor.
  228. */
  229. int curr_rx_urb_idx;
  230. u16 curr_rx_urb_offset;
  231. u8 rx_urb_filled[MAX_RX_URBS];
  232. struct tasklet_struct unthrottle_tasklet;
  233. struct work_struct retry_unthrottle_workqueue;
  234. };
  235. struct hso_device {
  236. union {
  237. struct hso_serial *dev_serial;
  238. struct hso_net *dev_net;
  239. } port_data;
  240. u32 port_spec;
  241. u8 is_active;
  242. u8 usb_gone;
  243. struct work_struct async_get_intf;
  244. struct work_struct async_put_intf;
  245. struct usb_device *usb;
  246. struct usb_interface *interface;
  247. struct device *dev;
  248. struct kref ref;
  249. struct mutex mutex;
  250. };
  251. /* Type of interface */
  252. #define HSO_INTF_MASK 0xFF00
  253. #define HSO_INTF_MUX 0x0100
  254. #define HSO_INTF_BULK 0x0200
  255. /* Type of port */
  256. #define HSO_PORT_MASK 0xFF
  257. #define HSO_PORT_NO_PORT 0x0
  258. #define HSO_PORT_CONTROL 0x1
  259. #define HSO_PORT_APP 0x2
  260. #define HSO_PORT_GPS 0x3
  261. #define HSO_PORT_PCSC 0x4
  262. #define HSO_PORT_APP2 0x5
  263. #define HSO_PORT_GPS_CONTROL 0x6
  264. #define HSO_PORT_MSD 0x7
  265. #define HSO_PORT_VOICE 0x8
  266. #define HSO_PORT_DIAG2 0x9
  267. #define HSO_PORT_DIAG 0x10
  268. #define HSO_PORT_MODEM 0x11
  269. #define HSO_PORT_NETWORK 0x12
  270. /* Additional device info */
  271. #define HSO_INFO_MASK 0xFF000000
  272. #define HSO_INFO_CRC_BUG 0x01000000
  273. /*****************************************************************************/
  274. /* Prototypes */
  275. /*****************************************************************************/
  276. /* Serial driver functions */
  277. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  278. unsigned int set, unsigned int clear);
  279. static void ctrl_callback(struct urb *urb);
  280. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  281. static void hso_kick_transmit(struct hso_serial *serial);
  282. /* Helper functions */
  283. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  284. struct usb_device *usb, gfp_t gfp);
  285. static void log_usb_status(int status, const char *function);
  286. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  287. int type, int dir);
  288. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  289. static void hso_free_interface(struct usb_interface *intf);
  290. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  291. static int hso_stop_serial_device(struct hso_device *hso_dev);
  292. static int hso_start_net_device(struct hso_device *hso_dev);
  293. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  294. static int hso_stop_net_device(struct hso_device *hso_dev);
  295. static void hso_serial_ref_free(struct kref *ref);
  296. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  297. static int hso_mux_serial_read(struct hso_serial *serial);
  298. static void async_get_intf(struct work_struct *data);
  299. static void async_put_intf(struct work_struct *data);
  300. static int hso_put_activity(struct hso_device *hso_dev);
  301. static int hso_get_activity(struct hso_device *hso_dev);
  302. static void tiocmget_intr_callback(struct urb *urb);
  303. /*****************************************************************************/
  304. /* Helping functions */
  305. /*****************************************************************************/
  306. /* #define DEBUG */
  307. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  308. {
  309. return hso_dev->port_data.dev_net;
  310. }
  311. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  312. {
  313. return hso_dev->port_data.dev_serial;
  314. }
  315. /* Debugging functions */
  316. #ifdef DEBUG
  317. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  318. unsigned int len)
  319. {
  320. static char name[255];
  321. sprintf(name, "hso[%d:%s]", line_count, func_name);
  322. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  323. }
  324. #define DUMP(buf_, len_) \
  325. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  326. #define DUMP1(buf_, len_) \
  327. do { \
  328. if (0x01 & debug) \
  329. DUMP(buf_, len_); \
  330. } while (0)
  331. #else
  332. #define DUMP(buf_, len_)
  333. #define DUMP1(buf_, len_)
  334. #endif
  335. /* module parameters */
  336. static int debug;
  337. static int tty_major;
  338. static int disable_net;
  339. /* driver info */
  340. static const char driver_name[] = "hso";
  341. static const char tty_filename[] = "ttyHS";
  342. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  343. /* the usb driver itself (registered in hso_init) */
  344. static struct usb_driver hso_driver;
  345. /* serial structures */
  346. static struct tty_driver *tty_drv;
  347. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  348. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  349. static spinlock_t serial_table_lock;
  350. static const s32 default_port_spec[] = {
  351. HSO_INTF_MUX | HSO_PORT_NETWORK,
  352. HSO_INTF_BULK | HSO_PORT_DIAG,
  353. HSO_INTF_BULK | HSO_PORT_MODEM,
  354. 0
  355. };
  356. static const s32 icon321_port_spec[] = {
  357. HSO_INTF_MUX | HSO_PORT_NETWORK,
  358. HSO_INTF_BULK | HSO_PORT_DIAG2,
  359. HSO_INTF_BULK | HSO_PORT_MODEM,
  360. HSO_INTF_BULK | HSO_PORT_DIAG,
  361. 0
  362. };
  363. #define default_port_device(vendor, product) \
  364. USB_DEVICE(vendor, product), \
  365. .driver_info = (kernel_ulong_t)default_port_spec
  366. #define icon321_port_device(vendor, product) \
  367. USB_DEVICE(vendor, product), \
  368. .driver_info = (kernel_ulong_t)icon321_port_spec
  369. /* list of devices we support */
  370. static const struct usb_device_id hso_ids[] = {
  371. {default_port_device(0x0af0, 0x6711)},
  372. {default_port_device(0x0af0, 0x6731)},
  373. {default_port_device(0x0af0, 0x6751)},
  374. {default_port_device(0x0af0, 0x6771)},
  375. {default_port_device(0x0af0, 0x6791)},
  376. {default_port_device(0x0af0, 0x6811)},
  377. {default_port_device(0x0af0, 0x6911)},
  378. {default_port_device(0x0af0, 0x6951)},
  379. {default_port_device(0x0af0, 0x6971)},
  380. {default_port_device(0x0af0, 0x7011)},
  381. {default_port_device(0x0af0, 0x7031)},
  382. {default_port_device(0x0af0, 0x7051)},
  383. {default_port_device(0x0af0, 0x7071)},
  384. {default_port_device(0x0af0, 0x7111)},
  385. {default_port_device(0x0af0, 0x7211)},
  386. {default_port_device(0x0af0, 0x7251)},
  387. {default_port_device(0x0af0, 0x7271)},
  388. {default_port_device(0x0af0, 0x7311)},
  389. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  390. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  391. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  392. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  393. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  395. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  396. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  397. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  398. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  399. {USB_DEVICE(0x0af0, 0x7701)},
  400. {USB_DEVICE(0x0af0, 0x7706)},
  401. {USB_DEVICE(0x0af0, 0x7801)},
  402. {USB_DEVICE(0x0af0, 0x7901)},
  403. {USB_DEVICE(0x0af0, 0x7A01)},
  404. {USB_DEVICE(0x0af0, 0x7A05)},
  405. {USB_DEVICE(0x0af0, 0x8200)},
  406. {USB_DEVICE(0x0af0, 0x8201)},
  407. {USB_DEVICE(0x0af0, 0x8300)},
  408. {USB_DEVICE(0x0af0, 0x8302)},
  409. {USB_DEVICE(0x0af0, 0x8304)},
  410. {USB_DEVICE(0x0af0, 0x8400)},
  411. {USB_DEVICE(0x0af0, 0xd035)},
  412. {USB_DEVICE(0x0af0, 0xd055)},
  413. {USB_DEVICE(0x0af0, 0xd155)},
  414. {USB_DEVICE(0x0af0, 0xd255)},
  415. {USB_DEVICE(0x0af0, 0xd057)},
  416. {USB_DEVICE(0x0af0, 0xd157)},
  417. {USB_DEVICE(0x0af0, 0xd257)},
  418. {USB_DEVICE(0x0af0, 0xd357)},
  419. {USB_DEVICE(0x0af0, 0xd058)},
  420. {USB_DEVICE(0x0af0, 0xc100)},
  421. {}
  422. };
  423. MODULE_DEVICE_TABLE(usb, hso_ids);
  424. /* Sysfs attribute */
  425. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  426. struct device_attribute *attr,
  427. char *buf)
  428. {
  429. struct hso_device *hso_dev = dev_get_drvdata(dev);
  430. char *port_name;
  431. if (!hso_dev)
  432. return 0;
  433. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  434. case HSO_PORT_CONTROL:
  435. port_name = "Control";
  436. break;
  437. case HSO_PORT_APP:
  438. port_name = "Application";
  439. break;
  440. case HSO_PORT_APP2:
  441. port_name = "Application2";
  442. break;
  443. case HSO_PORT_GPS:
  444. port_name = "GPS";
  445. break;
  446. case HSO_PORT_GPS_CONTROL:
  447. port_name = "GPS Control";
  448. break;
  449. case HSO_PORT_PCSC:
  450. port_name = "PCSC";
  451. break;
  452. case HSO_PORT_DIAG:
  453. port_name = "Diagnostic";
  454. break;
  455. case HSO_PORT_DIAG2:
  456. port_name = "Diagnostic2";
  457. break;
  458. case HSO_PORT_MODEM:
  459. port_name = "Modem";
  460. break;
  461. case HSO_PORT_NETWORK:
  462. port_name = "Network";
  463. break;
  464. default:
  465. port_name = "Unknown";
  466. break;
  467. }
  468. return sprintf(buf, "%s\n", port_name);
  469. }
  470. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  471. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  472. {
  473. int idx;
  474. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  475. if (serial->rx_urb[idx] == urb)
  476. return idx;
  477. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  478. return -1;
  479. }
  480. /* converts mux value to a port spec value */
  481. static u32 hso_mux_to_port(int mux)
  482. {
  483. u32 result;
  484. switch (mux) {
  485. case 0x1:
  486. result = HSO_PORT_CONTROL;
  487. break;
  488. case 0x2:
  489. result = HSO_PORT_APP;
  490. break;
  491. case 0x4:
  492. result = HSO_PORT_PCSC;
  493. break;
  494. case 0x8:
  495. result = HSO_PORT_GPS;
  496. break;
  497. case 0x10:
  498. result = HSO_PORT_APP2;
  499. break;
  500. default:
  501. result = HSO_PORT_NO_PORT;
  502. }
  503. return result;
  504. }
  505. /* converts port spec value to a mux value */
  506. static u32 hso_port_to_mux(int port)
  507. {
  508. u32 result;
  509. switch (port & HSO_PORT_MASK) {
  510. case HSO_PORT_CONTROL:
  511. result = 0x0;
  512. break;
  513. case HSO_PORT_APP:
  514. result = 0x1;
  515. break;
  516. case HSO_PORT_PCSC:
  517. result = 0x2;
  518. break;
  519. case HSO_PORT_GPS:
  520. result = 0x3;
  521. break;
  522. case HSO_PORT_APP2:
  523. result = 0x4;
  524. break;
  525. default:
  526. result = 0x0;
  527. }
  528. return result;
  529. }
  530. static struct hso_serial *get_serial_by_shared_int_and_type(
  531. struct hso_shared_int *shared_int,
  532. int mux)
  533. {
  534. int i, port;
  535. port = hso_mux_to_port(mux);
  536. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  537. if (serial_table[i] &&
  538. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  539. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  540. return dev2ser(serial_table[i]);
  541. }
  542. }
  543. return NULL;
  544. }
  545. static struct hso_serial *get_serial_by_index(unsigned index)
  546. {
  547. struct hso_serial *serial = NULL;
  548. unsigned long flags;
  549. spin_lock_irqsave(&serial_table_lock, flags);
  550. if (serial_table[index])
  551. serial = dev2ser(serial_table[index]);
  552. spin_unlock_irqrestore(&serial_table_lock, flags);
  553. return serial;
  554. }
  555. static int get_free_serial_index(void)
  556. {
  557. int index;
  558. unsigned long flags;
  559. spin_lock_irqsave(&serial_table_lock, flags);
  560. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  561. if (serial_table[index] == NULL) {
  562. spin_unlock_irqrestore(&serial_table_lock, flags);
  563. return index;
  564. }
  565. }
  566. spin_unlock_irqrestore(&serial_table_lock, flags);
  567. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  568. return -1;
  569. }
  570. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  571. {
  572. unsigned long flags;
  573. spin_lock_irqsave(&serial_table_lock, flags);
  574. if (serial)
  575. serial_table[index] = serial->parent;
  576. else
  577. serial_table[index] = NULL;
  578. spin_unlock_irqrestore(&serial_table_lock, flags);
  579. }
  580. /* log a meaningful explanation of an USB status */
  581. static void log_usb_status(int status, const char *function)
  582. {
  583. char *explanation;
  584. switch (status) {
  585. case -ENODEV:
  586. explanation = "no device";
  587. break;
  588. case -ENOENT:
  589. explanation = "endpoint not enabled";
  590. break;
  591. case -EPIPE:
  592. explanation = "endpoint stalled";
  593. break;
  594. case -ENOSPC:
  595. explanation = "not enough bandwidth";
  596. break;
  597. case -ESHUTDOWN:
  598. explanation = "device disabled";
  599. break;
  600. case -EHOSTUNREACH:
  601. explanation = "device suspended";
  602. break;
  603. case -EINVAL:
  604. case -EAGAIN:
  605. case -EFBIG:
  606. case -EMSGSIZE:
  607. explanation = "internal error";
  608. break;
  609. default:
  610. explanation = "unknown status";
  611. break;
  612. }
  613. D1("%s: received USB status - %s (%d)", function, explanation, status);
  614. }
  615. /* Network interface functions */
  616. /* called when net interface is brought up by ifconfig */
  617. static int hso_net_open(struct net_device *net)
  618. {
  619. struct hso_net *odev = netdev_priv(net);
  620. unsigned long flags = 0;
  621. if (!odev) {
  622. dev_err(&net->dev, "No net device !\n");
  623. return -ENODEV;
  624. }
  625. odev->skb_tx_buf = NULL;
  626. /* setup environment */
  627. spin_lock_irqsave(&odev->net_lock, flags);
  628. odev->rx_parse_state = WAIT_IP;
  629. odev->rx_buf_size = 0;
  630. odev->rx_buf_missing = sizeof(struct iphdr);
  631. spin_unlock_irqrestore(&odev->net_lock, flags);
  632. /* We are up and running. */
  633. set_bit(HSO_NET_RUNNING, &odev->flags);
  634. hso_start_net_device(odev->parent);
  635. /* Tell the kernel we are ready to start receiving from it */
  636. netif_start_queue(net);
  637. return 0;
  638. }
  639. /* called when interface is brought down by ifconfig */
  640. static int hso_net_close(struct net_device *net)
  641. {
  642. struct hso_net *odev = netdev_priv(net);
  643. /* we don't need the queue anymore */
  644. netif_stop_queue(net);
  645. /* no longer running */
  646. clear_bit(HSO_NET_RUNNING, &odev->flags);
  647. hso_stop_net_device(odev->parent);
  648. /* done */
  649. return 0;
  650. }
  651. /* USB tells is xmit done, we should start the netqueue again */
  652. static void write_bulk_callback(struct urb *urb)
  653. {
  654. struct hso_net *odev = urb->context;
  655. int status = urb->status;
  656. /* Sanity check */
  657. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  658. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  659. return;
  660. }
  661. /* Do we still have a valid kernel network device? */
  662. if (!netif_device_present(odev->net)) {
  663. dev_err(&urb->dev->dev, "%s: net device not present\n",
  664. __func__);
  665. return;
  666. }
  667. /* log status, but don't act on it, we don't need to resubmit anything
  668. * anyhow */
  669. if (status)
  670. log_usb_status(status, __func__);
  671. hso_put_activity(odev->parent);
  672. /* Tell the network interface we are ready for another frame */
  673. netif_wake_queue(odev->net);
  674. }
  675. /* called by kernel when we need to transmit a packet */
  676. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  677. struct net_device *net)
  678. {
  679. struct hso_net *odev = netdev_priv(net);
  680. int result;
  681. /* Tell the kernel, "No more frames 'til we are done with this one." */
  682. netif_stop_queue(net);
  683. if (hso_get_activity(odev->parent) == -EAGAIN) {
  684. odev->skb_tx_buf = skb;
  685. return NETDEV_TX_OK;
  686. }
  687. /* log if asked */
  688. DUMP1(skb->data, skb->len);
  689. /* Copy it from kernel memory to OUR memory */
  690. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  691. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  692. /* Fill in the URB for shipping it out. */
  693. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  694. odev->parent->usb,
  695. usb_sndbulkpipe(odev->parent->usb,
  696. odev->out_endp->
  697. bEndpointAddress & 0x7F),
  698. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  699. odev);
  700. /* Deal with the Zero Length packet problem, I hope */
  701. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  702. /* Send the URB on its merry way. */
  703. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  704. if (result) {
  705. dev_warn(&odev->parent->interface->dev,
  706. "failed mux_bulk_tx_urb %d", result);
  707. net->stats.tx_errors++;
  708. netif_start_queue(net);
  709. } else {
  710. net->stats.tx_packets++;
  711. net->stats.tx_bytes += skb->len;
  712. /* And tell the kernel when the last transmit started. */
  713. net->trans_start = jiffies;
  714. }
  715. dev_kfree_skb(skb);
  716. /* we're done */
  717. return NETDEV_TX_OK;
  718. }
  719. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  720. {
  721. struct hso_net *odev = netdev_priv(net);
  722. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  723. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  724. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  725. }
  726. static const struct ethtool_ops ops = {
  727. .get_drvinfo = hso_get_drvinfo,
  728. .get_link = ethtool_op_get_link
  729. };
  730. /* called when a packet did not ack after watchdogtimeout */
  731. static void hso_net_tx_timeout(struct net_device *net)
  732. {
  733. struct hso_net *odev = netdev_priv(net);
  734. if (!odev)
  735. return;
  736. /* Tell syslog we are hosed. */
  737. dev_warn(&net->dev, "Tx timed out.\n");
  738. /* Tear the waiting frame off the list */
  739. if (odev->mux_bulk_tx_urb &&
  740. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  741. usb_unlink_urb(odev->mux_bulk_tx_urb);
  742. /* Update statistics */
  743. net->stats.tx_errors++;
  744. }
  745. /* make a real packet from the received USB buffer */
  746. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  747. unsigned int count, unsigned char is_eop)
  748. {
  749. unsigned short temp_bytes;
  750. unsigned short buffer_offset = 0;
  751. unsigned short frame_len;
  752. unsigned char *tmp_rx_buf;
  753. /* log if needed */
  754. D1("Rx %d bytes", count);
  755. DUMP(ip_pkt, min(128, (int)count));
  756. while (count) {
  757. switch (odev->rx_parse_state) {
  758. case WAIT_IP:
  759. /* waiting for IP header. */
  760. /* wanted bytes - size of ip header */
  761. temp_bytes =
  762. (count <
  763. odev->rx_buf_missing) ? count : odev->
  764. rx_buf_missing;
  765. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  766. odev->rx_buf_size, ip_pkt + buffer_offset,
  767. temp_bytes);
  768. odev->rx_buf_size += temp_bytes;
  769. buffer_offset += temp_bytes;
  770. odev->rx_buf_missing -= temp_bytes;
  771. count -= temp_bytes;
  772. if (!odev->rx_buf_missing) {
  773. /* header is complete allocate an sk_buffer and
  774. * continue to WAIT_DATA */
  775. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  776. if ((frame_len > DEFAULT_MRU) ||
  777. (frame_len < sizeof(struct iphdr))) {
  778. dev_err(&odev->net->dev,
  779. "Invalid frame (%d) length\n",
  780. frame_len);
  781. odev->rx_parse_state = WAIT_SYNC;
  782. continue;
  783. }
  784. /* Allocate an sk_buff */
  785. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  786. frame_len);
  787. if (!odev->skb_rx_buf) {
  788. /* We got no receive buffer. */
  789. D1("could not allocate memory");
  790. odev->rx_parse_state = WAIT_SYNC;
  791. return;
  792. }
  793. /* Copy what we got so far. make room for iphdr
  794. * after tail. */
  795. tmp_rx_buf =
  796. skb_put(odev->skb_rx_buf,
  797. sizeof(struct iphdr));
  798. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  799. sizeof(struct iphdr));
  800. /* ETH_HLEN */
  801. odev->rx_buf_size = sizeof(struct iphdr);
  802. /* Filip actually use .tot_len */
  803. odev->rx_buf_missing =
  804. frame_len - sizeof(struct iphdr);
  805. odev->rx_parse_state = WAIT_DATA;
  806. }
  807. break;
  808. case WAIT_DATA:
  809. temp_bytes = (count < odev->rx_buf_missing)
  810. ? count : odev->rx_buf_missing;
  811. /* Copy the rest of the bytes that are left in the
  812. * buffer into the waiting sk_buf. */
  813. /* Make room for temp_bytes after tail. */
  814. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  815. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  816. odev->rx_buf_missing -= temp_bytes;
  817. count -= temp_bytes;
  818. buffer_offset += temp_bytes;
  819. odev->rx_buf_size += temp_bytes;
  820. if (!odev->rx_buf_missing) {
  821. /* Packet is complete. Inject into stack. */
  822. /* We have IP packet here */
  823. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  824. /* don't check it */
  825. odev->skb_rx_buf->ip_summed =
  826. CHECKSUM_UNNECESSARY;
  827. skb_reset_mac_header(odev->skb_rx_buf);
  828. /* Ship it off to the kernel */
  829. netif_rx(odev->skb_rx_buf);
  830. /* No longer our buffer. */
  831. odev->skb_rx_buf = NULL;
  832. /* update out statistics */
  833. odev->net->stats.rx_packets++;
  834. odev->net->stats.rx_bytes += odev->rx_buf_size;
  835. odev->rx_buf_size = 0;
  836. odev->rx_buf_missing = sizeof(struct iphdr);
  837. odev->rx_parse_state = WAIT_IP;
  838. }
  839. break;
  840. case WAIT_SYNC:
  841. D1(" W_S");
  842. count = 0;
  843. break;
  844. default:
  845. D1(" ");
  846. count--;
  847. break;
  848. }
  849. }
  850. /* Recovery mechanism for WAIT_SYNC state. */
  851. if (is_eop) {
  852. if (odev->rx_parse_state == WAIT_SYNC) {
  853. odev->rx_parse_state = WAIT_IP;
  854. odev->rx_buf_size = 0;
  855. odev->rx_buf_missing = sizeof(struct iphdr);
  856. }
  857. }
  858. }
  859. /* Moving data from usb to kernel (in interrupt state) */
  860. static void read_bulk_callback(struct urb *urb)
  861. {
  862. struct hso_net *odev = urb->context;
  863. struct net_device *net;
  864. int result;
  865. int status = urb->status;
  866. /* is al ok? (Filip: Who's Al ?) */
  867. if (status) {
  868. log_usb_status(status, __func__);
  869. return;
  870. }
  871. /* Sanity check */
  872. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  873. D1("BULK IN callback but driver is not active!");
  874. return;
  875. }
  876. usb_mark_last_busy(urb->dev);
  877. net = odev->net;
  878. if (!netif_device_present(net)) {
  879. /* Somebody killed our network interface... */
  880. return;
  881. }
  882. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  883. u32 rest;
  884. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  885. rest = urb->actual_length %
  886. le16_to_cpu(odev->in_endp->wMaxPacketSize);
  887. if (((rest == 5) || (rest == 6)) &&
  888. !memcmp(((u8 *) urb->transfer_buffer) +
  889. urb->actual_length - 4, crc_check, 4)) {
  890. urb->actual_length -= 4;
  891. }
  892. }
  893. /* do we even have a packet? */
  894. if (urb->actual_length) {
  895. /* Handle the IP stream, add header and push it onto network
  896. * stack if the packet is complete. */
  897. spin_lock(&odev->net_lock);
  898. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  899. (urb->transfer_buffer_length >
  900. urb->actual_length) ? 1 : 0);
  901. spin_unlock(&odev->net_lock);
  902. }
  903. /* We are done with this URB, resubmit it. Prep the USB to wait for
  904. * another frame. Reuse same as received. */
  905. usb_fill_bulk_urb(urb,
  906. odev->parent->usb,
  907. usb_rcvbulkpipe(odev->parent->usb,
  908. odev->in_endp->
  909. bEndpointAddress & 0x7F),
  910. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  911. read_bulk_callback, odev);
  912. /* Give this to the USB subsystem so it can tell us when more data
  913. * arrives. */
  914. result = usb_submit_urb(urb, GFP_ATOMIC);
  915. if (result)
  916. dev_warn(&odev->parent->interface->dev,
  917. "%s failed submit mux_bulk_rx_urb %d", __func__,
  918. result);
  919. }
  920. /* Serial driver functions */
  921. static void hso_init_termios(struct ktermios *termios)
  922. {
  923. /*
  924. * The default requirements for this device are:
  925. */
  926. termios->c_iflag &=
  927. ~(IGNBRK /* disable ignore break */
  928. | BRKINT /* disable break causes interrupt */
  929. | PARMRK /* disable mark parity errors */
  930. | ISTRIP /* disable clear high bit of input characters */
  931. | INLCR /* disable translate NL to CR */
  932. | IGNCR /* disable ignore CR */
  933. | ICRNL /* disable translate CR to NL */
  934. | IXON); /* disable enable XON/XOFF flow control */
  935. /* disable postprocess output characters */
  936. termios->c_oflag &= ~OPOST;
  937. termios->c_lflag &=
  938. ~(ECHO /* disable echo input characters */
  939. | ECHONL /* disable echo new line */
  940. | ICANON /* disable erase, kill, werase, and rprnt
  941. special characters */
  942. | ISIG /* disable interrupt, quit, and suspend special
  943. characters */
  944. | IEXTEN); /* disable non-POSIX special characters */
  945. termios->c_cflag &=
  946. ~(CSIZE /* no size */
  947. | PARENB /* disable parity bit */
  948. | CBAUD /* clear current baud rate */
  949. | CBAUDEX); /* clear current buad rate */
  950. termios->c_cflag |= CS8; /* character size 8 bits */
  951. /* baud rate 115200 */
  952. tty_termios_encode_baud_rate(termios, 115200, 115200);
  953. }
  954. static void _hso_serial_set_termios(struct tty_struct *tty,
  955. struct ktermios *old)
  956. {
  957. struct hso_serial *serial = get_serial_by_tty(tty);
  958. struct ktermios *termios;
  959. if (!serial) {
  960. printk(KERN_ERR "%s: no tty structures", __func__);
  961. return;
  962. }
  963. D4("port %d", serial->minor);
  964. /*
  965. * Fix up unsupported bits
  966. */
  967. termios = tty->termios;
  968. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  969. termios->c_cflag &=
  970. ~(CSIZE /* no size */
  971. | PARENB /* disable parity bit */
  972. | CBAUD /* clear current baud rate */
  973. | CBAUDEX); /* clear current buad rate */
  974. termios->c_cflag |= CS8; /* character size 8 bits */
  975. /* baud rate 115200 */
  976. tty_encode_baud_rate(tty, 115200, 115200);
  977. }
  978. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  979. {
  980. int result;
  981. #ifdef CONFIG_HSO_AUTOPM
  982. usb_mark_last_busy(urb->dev);
  983. #endif
  984. /* We are done with this URB, resubmit it. Prep the USB to wait for
  985. * another frame */
  986. usb_fill_bulk_urb(urb, serial->parent->usb,
  987. usb_rcvbulkpipe(serial->parent->usb,
  988. serial->in_endp->
  989. bEndpointAddress & 0x7F),
  990. urb->transfer_buffer, serial->rx_data_length,
  991. hso_std_serial_read_bulk_callback, serial);
  992. /* Give this to the USB subsystem so it can tell us when more data
  993. * arrives. */
  994. result = usb_submit_urb(urb, GFP_ATOMIC);
  995. if (result) {
  996. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  997. __func__, result);
  998. }
  999. }
  1000. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1001. {
  1002. int count;
  1003. struct urb *curr_urb;
  1004. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1005. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1006. count = put_rxbuf_data(curr_urb, serial);
  1007. if (count == -1)
  1008. return;
  1009. if (count == 0) {
  1010. serial->curr_rx_urb_idx++;
  1011. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1012. serial->curr_rx_urb_idx = 0;
  1013. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1014. }
  1015. }
  1016. }
  1017. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1018. {
  1019. int count = 0;
  1020. struct urb *urb;
  1021. urb = serial->rx_urb[0];
  1022. if (serial->open_count > 0) {
  1023. count = put_rxbuf_data(urb, serial);
  1024. if (count == -1)
  1025. return;
  1026. }
  1027. /* Re issue a read as long as we receive data. */
  1028. if (count == 0 && ((urb->actual_length != 0) ||
  1029. (serial->rx_state == RX_PENDING))) {
  1030. serial->rx_state = RX_SENT;
  1031. hso_mux_serial_read(serial);
  1032. } else
  1033. serial->rx_state = RX_IDLE;
  1034. }
  1035. /* read callback for Diag and CS port */
  1036. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1037. {
  1038. struct hso_serial *serial = urb->context;
  1039. int status = urb->status;
  1040. /* sanity check */
  1041. if (!serial) {
  1042. D1("serial == NULL");
  1043. return;
  1044. } else if (status) {
  1045. log_usb_status(status, __func__);
  1046. return;
  1047. }
  1048. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1049. D1("Actual length = %d\n", urb->actual_length);
  1050. DUMP1(urb->transfer_buffer, urb->actual_length);
  1051. /* Anyone listening? */
  1052. if (serial->open_count == 0)
  1053. return;
  1054. if (status == 0) {
  1055. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1056. u32 rest;
  1057. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1058. rest =
  1059. urb->actual_length %
  1060. le16_to_cpu(serial->in_endp->wMaxPacketSize);
  1061. if (((rest == 5) || (rest == 6)) &&
  1062. !memcmp(((u8 *) urb->transfer_buffer) +
  1063. urb->actual_length - 4, crc_check, 4)) {
  1064. urb->actual_length -= 4;
  1065. }
  1066. }
  1067. /* Valid data, handle RX data */
  1068. spin_lock(&serial->serial_lock);
  1069. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1070. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1071. spin_unlock(&serial->serial_lock);
  1072. } else if (status == -ENOENT || status == -ECONNRESET) {
  1073. /* Unlinked - check for throttled port. */
  1074. D2("Port %d, successfully unlinked urb", serial->minor);
  1075. spin_lock(&serial->serial_lock);
  1076. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1077. hso_resubmit_rx_bulk_urb(serial, urb);
  1078. spin_unlock(&serial->serial_lock);
  1079. } else {
  1080. D2("Port %d, status = %d for read urb", serial->minor, status);
  1081. return;
  1082. }
  1083. }
  1084. /*
  1085. * This needs to be a tasklet otherwise we will
  1086. * end up recursively calling this function.
  1087. */
  1088. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1089. {
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&serial->serial_lock, flags);
  1092. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1093. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1094. else
  1095. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1096. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1097. }
  1098. static void hso_unthrottle(struct tty_struct *tty)
  1099. {
  1100. struct hso_serial *serial = get_serial_by_tty(tty);
  1101. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1102. }
  1103. static void hso_unthrottle_workfunc(struct work_struct *work)
  1104. {
  1105. struct hso_serial *serial =
  1106. container_of(work, struct hso_serial,
  1107. retry_unthrottle_workqueue);
  1108. hso_unthrottle_tasklet(serial);
  1109. }
  1110. /* open the requested serial port */
  1111. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1112. {
  1113. struct hso_serial *serial = get_serial_by_index(tty->index);
  1114. int result;
  1115. /* sanity check */
  1116. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1117. WARN_ON(1);
  1118. tty->driver_data = NULL;
  1119. D1("Failed to open port");
  1120. return -ENODEV;
  1121. }
  1122. mutex_lock(&serial->parent->mutex);
  1123. result = usb_autopm_get_interface(serial->parent->interface);
  1124. if (result < 0)
  1125. goto err_out;
  1126. D1("Opening %d", serial->minor);
  1127. kref_get(&serial->parent->ref);
  1128. /* setup */
  1129. spin_lock_irq(&serial->serial_lock);
  1130. tty->driver_data = serial;
  1131. tty_kref_put(serial->tty);
  1132. serial->tty = tty_kref_get(tty);
  1133. spin_unlock_irq(&serial->serial_lock);
  1134. /* check for port already opened, if not set the termios */
  1135. serial->open_count++;
  1136. if (serial->open_count == 1) {
  1137. tty->low_latency = 1;
  1138. serial->rx_state = RX_IDLE;
  1139. /* Force default termio settings */
  1140. _hso_serial_set_termios(tty, NULL);
  1141. tasklet_init(&serial->unthrottle_tasklet,
  1142. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1143. (unsigned long)serial);
  1144. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1145. hso_unthrottle_workfunc);
  1146. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1147. if (result) {
  1148. hso_stop_serial_device(serial->parent);
  1149. serial->open_count--;
  1150. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1151. }
  1152. } else {
  1153. D1("Port was already open");
  1154. }
  1155. usb_autopm_put_interface(serial->parent->interface);
  1156. /* done */
  1157. if (result)
  1158. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1159. err_out:
  1160. mutex_unlock(&serial->parent->mutex);
  1161. return result;
  1162. }
  1163. /* close the requested serial port */
  1164. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1165. {
  1166. struct hso_serial *serial = tty->driver_data;
  1167. u8 usb_gone;
  1168. D1("Closing serial port");
  1169. /* Open failed, no close cleanup required */
  1170. if (serial == NULL)
  1171. return;
  1172. mutex_lock(&serial->parent->mutex);
  1173. usb_gone = serial->parent->usb_gone;
  1174. if (!usb_gone)
  1175. usb_autopm_get_interface(serial->parent->interface);
  1176. /* reset the rts and dtr */
  1177. /* do the actual close */
  1178. serial->open_count--;
  1179. if (serial->open_count <= 0) {
  1180. serial->open_count = 0;
  1181. spin_lock_irq(&serial->serial_lock);
  1182. if (serial->tty == tty) {
  1183. serial->tty->driver_data = NULL;
  1184. serial->tty = NULL;
  1185. tty_kref_put(tty);
  1186. }
  1187. spin_unlock_irq(&serial->serial_lock);
  1188. if (!usb_gone)
  1189. hso_stop_serial_device(serial->parent);
  1190. tasklet_kill(&serial->unthrottle_tasklet);
  1191. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1192. }
  1193. if (!usb_gone)
  1194. usb_autopm_put_interface(serial->parent->interface);
  1195. mutex_unlock(&serial->parent->mutex);
  1196. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1197. }
  1198. /* close the requested serial port */
  1199. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1200. int count)
  1201. {
  1202. struct hso_serial *serial = get_serial_by_tty(tty);
  1203. int space, tx_bytes;
  1204. unsigned long flags;
  1205. /* sanity check */
  1206. if (serial == NULL) {
  1207. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1208. return -ENODEV;
  1209. }
  1210. spin_lock_irqsave(&serial->serial_lock, flags);
  1211. space = serial->tx_data_length - serial->tx_buffer_count;
  1212. tx_bytes = (count < space) ? count : space;
  1213. if (!tx_bytes)
  1214. goto out;
  1215. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1216. serial->tx_buffer_count += tx_bytes;
  1217. out:
  1218. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1219. hso_kick_transmit(serial);
  1220. /* done */
  1221. return tx_bytes;
  1222. }
  1223. /* how much room is there for writing */
  1224. static int hso_serial_write_room(struct tty_struct *tty)
  1225. {
  1226. struct hso_serial *serial = get_serial_by_tty(tty);
  1227. int room;
  1228. unsigned long flags;
  1229. spin_lock_irqsave(&serial->serial_lock, flags);
  1230. room = serial->tx_data_length - serial->tx_buffer_count;
  1231. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1232. /* return free room */
  1233. return room;
  1234. }
  1235. /* setup the term */
  1236. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1237. {
  1238. struct hso_serial *serial = get_serial_by_tty(tty);
  1239. unsigned long flags;
  1240. if (old)
  1241. D5("Termios called with: cflags new[%d] - old[%d]",
  1242. tty->termios->c_cflag, old->c_cflag);
  1243. /* the actual setup */
  1244. spin_lock_irqsave(&serial->serial_lock, flags);
  1245. if (serial->open_count)
  1246. _hso_serial_set_termios(tty, old);
  1247. else
  1248. tty->termios = old;
  1249. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1250. /* done */
  1251. return;
  1252. }
  1253. /* how many characters in the buffer */
  1254. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1255. {
  1256. struct hso_serial *serial = get_serial_by_tty(tty);
  1257. int chars;
  1258. unsigned long flags;
  1259. /* sanity check */
  1260. if (serial == NULL)
  1261. return 0;
  1262. spin_lock_irqsave(&serial->serial_lock, flags);
  1263. chars = serial->tx_buffer_count;
  1264. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1265. return chars;
  1266. }
  1267. static int tiocmget_submit_urb(struct hso_serial *serial,
  1268. struct hso_tiocmget *tiocmget,
  1269. struct usb_device *usb)
  1270. {
  1271. int result;
  1272. if (serial->parent->usb_gone)
  1273. return -ENODEV;
  1274. usb_fill_int_urb(tiocmget->urb, usb,
  1275. usb_rcvintpipe(usb,
  1276. tiocmget->endp->
  1277. bEndpointAddress & 0x7F),
  1278. &tiocmget->serial_state_notification,
  1279. sizeof(struct hso_serial_state_notification),
  1280. tiocmget_intr_callback, serial,
  1281. tiocmget->endp->bInterval);
  1282. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1283. if (result) {
  1284. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1285. result);
  1286. }
  1287. return result;
  1288. }
  1289. static void tiocmget_intr_callback(struct urb *urb)
  1290. {
  1291. struct hso_serial *serial = urb->context;
  1292. struct hso_tiocmget *tiocmget;
  1293. int status = urb->status;
  1294. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1295. struct uart_icount *icount;
  1296. struct hso_serial_state_notification *serial_state_notification;
  1297. struct usb_device *usb;
  1298. /* Sanity checks */
  1299. if (!serial)
  1300. return;
  1301. if (status) {
  1302. log_usb_status(status, __func__);
  1303. return;
  1304. }
  1305. tiocmget = serial->tiocmget;
  1306. if (!tiocmget)
  1307. return;
  1308. usb = serial->parent->usb;
  1309. serial_state_notification = &tiocmget->serial_state_notification;
  1310. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1311. serial_state_notification->bNotification != B_NOTIFICATION ||
  1312. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1313. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1314. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1315. dev_warn(&usb->dev,
  1316. "hso received invalid serial state notification\n");
  1317. DUMP(serial_state_notification,
  1318. sizeof(struct hso_serial_state_notification));
  1319. } else {
  1320. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1321. UART_state_bitmap);
  1322. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1323. icount = &tiocmget->icount;
  1324. spin_lock(&serial->serial_lock);
  1325. if ((UART_state_bitmap & B_OVERRUN) !=
  1326. (prev_UART_state_bitmap & B_OVERRUN))
  1327. icount->parity++;
  1328. if ((UART_state_bitmap & B_PARITY) !=
  1329. (prev_UART_state_bitmap & B_PARITY))
  1330. icount->parity++;
  1331. if ((UART_state_bitmap & B_FRAMING) !=
  1332. (prev_UART_state_bitmap & B_FRAMING))
  1333. icount->frame++;
  1334. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1335. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1336. icount->rng++;
  1337. if ((UART_state_bitmap & B_BREAK) !=
  1338. (prev_UART_state_bitmap & B_BREAK))
  1339. icount->brk++;
  1340. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1341. (prev_UART_state_bitmap & B_TX_CARRIER))
  1342. icount->dsr++;
  1343. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1344. (prev_UART_state_bitmap & B_RX_CARRIER))
  1345. icount->dcd++;
  1346. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1347. spin_unlock(&serial->serial_lock);
  1348. tiocmget->intr_completed = 1;
  1349. wake_up_interruptible(&tiocmget->waitq);
  1350. }
  1351. memset(serial_state_notification, 0,
  1352. sizeof(struct hso_serial_state_notification));
  1353. tiocmget_submit_urb(serial,
  1354. tiocmget,
  1355. serial->parent->usb);
  1356. }
  1357. /*
  1358. * next few functions largely stolen from drivers/serial/serial_core.c
  1359. */
  1360. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1361. * - mask passed in arg for lines of interest
  1362. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1363. * Caller should use TIOCGICOUNT to see which one it was
  1364. */
  1365. static int
  1366. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1367. {
  1368. DECLARE_WAITQUEUE(wait, current);
  1369. struct uart_icount cprev, cnow;
  1370. struct hso_tiocmget *tiocmget;
  1371. int ret;
  1372. tiocmget = serial->tiocmget;
  1373. if (!tiocmget)
  1374. return -ENOENT;
  1375. /*
  1376. * note the counters on entry
  1377. */
  1378. spin_lock_irq(&serial->serial_lock);
  1379. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1380. spin_unlock_irq(&serial->serial_lock);
  1381. add_wait_queue(&tiocmget->waitq, &wait);
  1382. for (;;) {
  1383. spin_lock_irq(&serial->serial_lock);
  1384. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1385. spin_unlock_irq(&serial->serial_lock);
  1386. set_current_state(TASK_INTERRUPTIBLE);
  1387. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1388. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1389. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1390. ret = 0;
  1391. break;
  1392. }
  1393. schedule();
  1394. /* see if a signal did it */
  1395. if (signal_pending(current)) {
  1396. ret = -ERESTARTSYS;
  1397. break;
  1398. }
  1399. cprev = cnow;
  1400. }
  1401. current->state = TASK_RUNNING;
  1402. remove_wait_queue(&tiocmget->waitq, &wait);
  1403. return ret;
  1404. }
  1405. /*
  1406. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1407. * Return: write counters to the user passed counter struct
  1408. * NB: both 1->0 and 0->1 transitions are counted except for
  1409. * RI where only 0->1 is counted.
  1410. */
  1411. static int hso_get_count(struct hso_serial *serial,
  1412. struct serial_icounter_struct __user *icnt)
  1413. {
  1414. struct serial_icounter_struct icount;
  1415. struct uart_icount cnow;
  1416. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1417. if (!tiocmget)
  1418. return -ENOENT;
  1419. spin_lock_irq(&serial->serial_lock);
  1420. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1421. spin_unlock_irq(&serial->serial_lock);
  1422. icount.cts = cnow.cts;
  1423. icount.dsr = cnow.dsr;
  1424. icount.rng = cnow.rng;
  1425. icount.dcd = cnow.dcd;
  1426. icount.rx = cnow.rx;
  1427. icount.tx = cnow.tx;
  1428. icount.frame = cnow.frame;
  1429. icount.overrun = cnow.overrun;
  1430. icount.parity = cnow.parity;
  1431. icount.brk = cnow.brk;
  1432. icount.buf_overrun = cnow.buf_overrun;
  1433. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1434. }
  1435. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1436. {
  1437. int retval;
  1438. struct hso_serial *serial = get_serial_by_tty(tty);
  1439. struct hso_tiocmget *tiocmget;
  1440. u16 UART_state_bitmap;
  1441. /* sanity check */
  1442. if (!serial) {
  1443. D1("no tty structures");
  1444. return -EINVAL;
  1445. }
  1446. spin_lock_irq(&serial->serial_lock);
  1447. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1448. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1449. tiocmget = serial->tiocmget;
  1450. if (tiocmget) {
  1451. UART_state_bitmap = le16_to_cpu(
  1452. tiocmget->prev_UART_state_bitmap);
  1453. if (UART_state_bitmap & B_RING_SIGNAL)
  1454. retval |= TIOCM_RNG;
  1455. if (UART_state_bitmap & B_RX_CARRIER)
  1456. retval |= TIOCM_CD;
  1457. if (UART_state_bitmap & B_TX_CARRIER)
  1458. retval |= TIOCM_DSR;
  1459. }
  1460. spin_unlock_irq(&serial->serial_lock);
  1461. return retval;
  1462. }
  1463. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1464. unsigned int set, unsigned int clear)
  1465. {
  1466. int val = 0;
  1467. unsigned long flags;
  1468. int if_num;
  1469. struct hso_serial *serial = get_serial_by_tty(tty);
  1470. /* sanity check */
  1471. if (!serial) {
  1472. D1("no tty structures");
  1473. return -EINVAL;
  1474. }
  1475. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1476. spin_lock_irqsave(&serial->serial_lock, flags);
  1477. if (set & TIOCM_RTS)
  1478. serial->rts_state = 1;
  1479. if (set & TIOCM_DTR)
  1480. serial->dtr_state = 1;
  1481. if (clear & TIOCM_RTS)
  1482. serial->rts_state = 0;
  1483. if (clear & TIOCM_DTR)
  1484. serial->dtr_state = 0;
  1485. if (serial->dtr_state)
  1486. val |= 0x01;
  1487. if (serial->rts_state)
  1488. val |= 0x02;
  1489. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1490. return usb_control_msg(serial->parent->usb,
  1491. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1492. 0x21, val, if_num, NULL, 0,
  1493. USB_CTRL_SET_TIMEOUT);
  1494. }
  1495. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1496. unsigned int cmd, unsigned long arg)
  1497. {
  1498. struct hso_serial *serial = get_serial_by_tty(tty);
  1499. void __user *uarg = (void __user *)arg;
  1500. int ret = 0;
  1501. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1502. if (!serial)
  1503. return -ENODEV;
  1504. switch (cmd) {
  1505. case TIOCMIWAIT:
  1506. ret = hso_wait_modem_status(serial, arg);
  1507. break;
  1508. case TIOCGICOUNT:
  1509. ret = hso_get_count(serial, uarg);
  1510. break;
  1511. default:
  1512. ret = -ENOIOCTLCMD;
  1513. break;
  1514. }
  1515. return ret;
  1516. }
  1517. /* starts a transmit */
  1518. static void hso_kick_transmit(struct hso_serial *serial)
  1519. {
  1520. u8 *temp;
  1521. unsigned long flags;
  1522. int res;
  1523. spin_lock_irqsave(&serial->serial_lock, flags);
  1524. if (!serial->tx_buffer_count)
  1525. goto out;
  1526. if (serial->tx_urb_used)
  1527. goto out;
  1528. /* Wakeup USB interface if necessary */
  1529. if (hso_get_activity(serial->parent) == -EAGAIN)
  1530. goto out;
  1531. /* Switch pointers around to avoid memcpy */
  1532. temp = serial->tx_buffer;
  1533. serial->tx_buffer = serial->tx_data;
  1534. serial->tx_data = temp;
  1535. serial->tx_data_count = serial->tx_buffer_count;
  1536. serial->tx_buffer_count = 0;
  1537. /* If temp is set, it means we switched buffers */
  1538. if (temp && serial->write_data) {
  1539. res = serial->write_data(serial);
  1540. if (res >= 0)
  1541. serial->tx_urb_used = 1;
  1542. }
  1543. out:
  1544. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1545. }
  1546. /* make a request (for reading and writing data to muxed serial port) */
  1547. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1548. struct urb *ctrl_urb,
  1549. struct usb_ctrlrequest *ctrl_req,
  1550. u8 *ctrl_urb_data, u32 size)
  1551. {
  1552. int result;
  1553. int pipe;
  1554. /* Sanity check */
  1555. if (!serial || !ctrl_urb || !ctrl_req) {
  1556. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1557. return -EINVAL;
  1558. }
  1559. /* initialize */
  1560. ctrl_req->wValue = 0;
  1561. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1562. ctrl_req->wLength = cpu_to_le16(size);
  1563. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1564. /* Reading command */
  1565. ctrl_req->bRequestType = USB_DIR_IN |
  1566. USB_TYPE_OPTION_VENDOR |
  1567. USB_RECIP_INTERFACE;
  1568. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1569. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1570. } else {
  1571. /* Writing command */
  1572. ctrl_req->bRequestType = USB_DIR_OUT |
  1573. USB_TYPE_OPTION_VENDOR |
  1574. USB_RECIP_INTERFACE;
  1575. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1576. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1577. }
  1578. /* syslog */
  1579. D2("%s command (%02x) len: %d, port: %d",
  1580. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1581. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1582. /* Load ctrl urb */
  1583. ctrl_urb->transfer_flags = 0;
  1584. usb_fill_control_urb(ctrl_urb,
  1585. serial->parent->usb,
  1586. pipe,
  1587. (u8 *) ctrl_req,
  1588. ctrl_urb_data, size, ctrl_callback, serial);
  1589. /* Send it on merry way */
  1590. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1591. if (result) {
  1592. dev_err(&ctrl_urb->dev->dev,
  1593. "%s failed submit ctrl_urb %d type %d", __func__,
  1594. result, type);
  1595. return result;
  1596. }
  1597. /* done */
  1598. return size;
  1599. }
  1600. /* called by intr_callback when read occurs */
  1601. static int hso_mux_serial_read(struct hso_serial *serial)
  1602. {
  1603. if (!serial)
  1604. return -EINVAL;
  1605. /* clean data */
  1606. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1607. /* make the request */
  1608. if (serial->num_rx_urbs != 1) {
  1609. dev_err(&serial->parent->interface->dev,
  1610. "ERROR: mux'd reads with multiple buffers "
  1611. "not possible\n");
  1612. return 0;
  1613. }
  1614. return mux_device_request(serial,
  1615. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1616. serial->parent->port_spec & HSO_PORT_MASK,
  1617. serial->rx_urb[0],
  1618. &serial->ctrl_req_rx,
  1619. serial->rx_data[0], serial->rx_data_length);
  1620. }
  1621. /* used for muxed serial port callback (muxed serial read) */
  1622. static void intr_callback(struct urb *urb)
  1623. {
  1624. struct hso_shared_int *shared_int = urb->context;
  1625. struct hso_serial *serial;
  1626. unsigned char *port_req;
  1627. int status = urb->status;
  1628. int i;
  1629. usb_mark_last_busy(urb->dev);
  1630. /* sanity check */
  1631. if (!shared_int)
  1632. return;
  1633. /* status check */
  1634. if (status) {
  1635. log_usb_status(status, __func__);
  1636. return;
  1637. }
  1638. D4("\n--- Got intr callback 0x%02X ---", status);
  1639. /* what request? */
  1640. port_req = urb->transfer_buffer;
  1641. D4(" port_req = 0x%.2X\n", *port_req);
  1642. /* loop over all muxed ports to find the one sending this */
  1643. for (i = 0; i < 8; i++) {
  1644. /* max 8 channels on MUX */
  1645. if (*port_req & (1 << i)) {
  1646. serial = get_serial_by_shared_int_and_type(shared_int,
  1647. (1 << i));
  1648. if (serial != NULL) {
  1649. D1("Pending read interrupt on port %d\n", i);
  1650. spin_lock(&serial->serial_lock);
  1651. if (serial->rx_state == RX_IDLE) {
  1652. /* Setup and send a ctrl req read on
  1653. * port i */
  1654. if (!serial->rx_urb_filled[0]) {
  1655. serial->rx_state = RX_SENT;
  1656. hso_mux_serial_read(serial);
  1657. } else
  1658. serial->rx_state = RX_PENDING;
  1659. } else {
  1660. D1("Already pending a read on "
  1661. "port %d\n", i);
  1662. }
  1663. spin_unlock(&serial->serial_lock);
  1664. }
  1665. }
  1666. }
  1667. /* Resubmit interrupt urb */
  1668. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1669. }
  1670. /* called for writing to muxed serial port */
  1671. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1672. {
  1673. if (NULL == serial)
  1674. return -EINVAL;
  1675. return mux_device_request(serial,
  1676. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1677. serial->parent->port_spec & HSO_PORT_MASK,
  1678. serial->tx_urb,
  1679. &serial->ctrl_req_tx,
  1680. serial->tx_data, serial->tx_data_count);
  1681. }
  1682. /* write callback for Diag and CS port */
  1683. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1684. {
  1685. struct hso_serial *serial = urb->context;
  1686. int status = urb->status;
  1687. struct tty_struct *tty;
  1688. /* sanity check */
  1689. if (!serial) {
  1690. D1("serial == NULL");
  1691. return;
  1692. }
  1693. spin_lock(&serial->serial_lock);
  1694. serial->tx_urb_used = 0;
  1695. tty = tty_kref_get(serial->tty);
  1696. spin_unlock(&serial->serial_lock);
  1697. if (status) {
  1698. log_usb_status(status, __func__);
  1699. tty_kref_put(tty);
  1700. return;
  1701. }
  1702. hso_put_activity(serial->parent);
  1703. if (tty) {
  1704. tty_wakeup(tty);
  1705. tty_kref_put(tty);
  1706. }
  1707. hso_kick_transmit(serial);
  1708. D1(" ");
  1709. return;
  1710. }
  1711. /* called for writing diag or CS serial port */
  1712. static int hso_std_serial_write_data(struct hso_serial *serial)
  1713. {
  1714. int count = serial->tx_data_count;
  1715. int result;
  1716. usb_fill_bulk_urb(serial->tx_urb,
  1717. serial->parent->usb,
  1718. usb_sndbulkpipe(serial->parent->usb,
  1719. serial->out_endp->
  1720. bEndpointAddress & 0x7F),
  1721. serial->tx_data, serial->tx_data_count,
  1722. hso_std_serial_write_bulk_callback, serial);
  1723. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1724. if (result) {
  1725. dev_warn(&serial->parent->usb->dev,
  1726. "Failed to submit urb - res %d\n", result);
  1727. return result;
  1728. }
  1729. return count;
  1730. }
  1731. /* callback after read or write on muxed serial port */
  1732. static void ctrl_callback(struct urb *urb)
  1733. {
  1734. struct hso_serial *serial = urb->context;
  1735. struct usb_ctrlrequest *req;
  1736. int status = urb->status;
  1737. struct tty_struct *tty;
  1738. /* sanity check */
  1739. if (!serial)
  1740. return;
  1741. spin_lock(&serial->serial_lock);
  1742. serial->tx_urb_used = 0;
  1743. tty = tty_kref_get(serial->tty);
  1744. spin_unlock(&serial->serial_lock);
  1745. if (status) {
  1746. log_usb_status(status, __func__);
  1747. tty_kref_put(tty);
  1748. return;
  1749. }
  1750. /* what request? */
  1751. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1752. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1753. D4("Actual length of urb = %d\n", urb->actual_length);
  1754. DUMP1(urb->transfer_buffer, urb->actual_length);
  1755. if (req->bRequestType ==
  1756. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1757. /* response to a read command */
  1758. serial->rx_urb_filled[0] = 1;
  1759. spin_lock(&serial->serial_lock);
  1760. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1761. spin_unlock(&serial->serial_lock);
  1762. } else {
  1763. hso_put_activity(serial->parent);
  1764. if (tty)
  1765. tty_wakeup(tty);
  1766. /* response to a write command */
  1767. hso_kick_transmit(serial);
  1768. }
  1769. tty_kref_put(tty);
  1770. }
  1771. /* handle RX data for serial port */
  1772. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1773. {
  1774. struct tty_struct *tty;
  1775. int write_length_remaining = 0;
  1776. int curr_write_len;
  1777. /* Sanity check */
  1778. if (urb == NULL || serial == NULL) {
  1779. D1("serial = NULL");
  1780. return -2;
  1781. }
  1782. /* All callers to put_rxbuf_data hold serial_lock */
  1783. tty = tty_kref_get(serial->tty);
  1784. /* Push data to tty */
  1785. if (tty) {
  1786. write_length_remaining = urb->actual_length -
  1787. serial->curr_rx_urb_offset;
  1788. D1("data to push to tty");
  1789. while (write_length_remaining) {
  1790. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1791. tty_kref_put(tty);
  1792. return -1;
  1793. }
  1794. curr_write_len = tty_insert_flip_string
  1795. (tty, urb->transfer_buffer +
  1796. serial->curr_rx_urb_offset,
  1797. write_length_remaining);
  1798. serial->curr_rx_urb_offset += curr_write_len;
  1799. write_length_remaining -= curr_write_len;
  1800. tty_flip_buffer_push(tty);
  1801. }
  1802. }
  1803. if (write_length_remaining == 0) {
  1804. serial->curr_rx_urb_offset = 0;
  1805. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1806. }
  1807. tty_kref_put(tty);
  1808. return write_length_remaining;
  1809. }
  1810. /* Base driver functions */
  1811. static void hso_log_port(struct hso_device *hso_dev)
  1812. {
  1813. char *port_type;
  1814. char port_dev[20];
  1815. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1816. case HSO_PORT_CONTROL:
  1817. port_type = "Control";
  1818. break;
  1819. case HSO_PORT_APP:
  1820. port_type = "Application";
  1821. break;
  1822. case HSO_PORT_GPS:
  1823. port_type = "GPS";
  1824. break;
  1825. case HSO_PORT_GPS_CONTROL:
  1826. port_type = "GPS control";
  1827. break;
  1828. case HSO_PORT_APP2:
  1829. port_type = "Application2";
  1830. break;
  1831. case HSO_PORT_PCSC:
  1832. port_type = "PCSC";
  1833. break;
  1834. case HSO_PORT_DIAG:
  1835. port_type = "Diagnostic";
  1836. break;
  1837. case HSO_PORT_DIAG2:
  1838. port_type = "Diagnostic2";
  1839. break;
  1840. case HSO_PORT_MODEM:
  1841. port_type = "Modem";
  1842. break;
  1843. case HSO_PORT_NETWORK:
  1844. port_type = "Network";
  1845. break;
  1846. default:
  1847. port_type = "Unknown";
  1848. break;
  1849. }
  1850. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1851. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1852. } else
  1853. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1854. dev2ser(hso_dev)->minor);
  1855. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1856. port_type, port_dev);
  1857. }
  1858. static int hso_start_net_device(struct hso_device *hso_dev)
  1859. {
  1860. int i, result = 0;
  1861. struct hso_net *hso_net = dev2net(hso_dev);
  1862. if (!hso_net)
  1863. return -ENODEV;
  1864. /* send URBs for all read buffers */
  1865. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1866. /* Prep a receive URB */
  1867. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1868. hso_dev->usb,
  1869. usb_rcvbulkpipe(hso_dev->usb,
  1870. hso_net->in_endp->
  1871. bEndpointAddress & 0x7F),
  1872. hso_net->mux_bulk_rx_buf_pool[i],
  1873. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1874. hso_net);
  1875. /* Put it out there so the device can send us stuff */
  1876. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1877. GFP_NOIO);
  1878. if (result)
  1879. dev_warn(&hso_dev->usb->dev,
  1880. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1881. i, result);
  1882. }
  1883. return result;
  1884. }
  1885. static int hso_stop_net_device(struct hso_device *hso_dev)
  1886. {
  1887. int i;
  1888. struct hso_net *hso_net = dev2net(hso_dev);
  1889. if (!hso_net)
  1890. return -ENODEV;
  1891. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1892. if (hso_net->mux_bulk_rx_urb_pool[i])
  1893. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1894. }
  1895. if (hso_net->mux_bulk_tx_urb)
  1896. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1897. return 0;
  1898. }
  1899. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1900. {
  1901. int i, result = 0;
  1902. struct hso_serial *serial = dev2ser(hso_dev);
  1903. if (!serial)
  1904. return -ENODEV;
  1905. /* If it is not the MUX port fill in and submit a bulk urb (already
  1906. * allocated in hso_serial_start) */
  1907. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1908. for (i = 0; i < serial->num_rx_urbs; i++) {
  1909. usb_fill_bulk_urb(serial->rx_urb[i],
  1910. serial->parent->usb,
  1911. usb_rcvbulkpipe(serial->parent->usb,
  1912. serial->in_endp->
  1913. bEndpointAddress &
  1914. 0x7F),
  1915. serial->rx_data[i],
  1916. serial->rx_data_length,
  1917. hso_std_serial_read_bulk_callback,
  1918. serial);
  1919. result = usb_submit_urb(serial->rx_urb[i], flags);
  1920. if (result) {
  1921. dev_warn(&serial->parent->usb->dev,
  1922. "Failed to submit urb - res %d\n",
  1923. result);
  1924. break;
  1925. }
  1926. }
  1927. } else {
  1928. mutex_lock(&serial->shared_int->shared_int_lock);
  1929. if (!serial->shared_int->use_count) {
  1930. result =
  1931. hso_mux_submit_intr_urb(serial->shared_int,
  1932. hso_dev->usb, flags);
  1933. }
  1934. serial->shared_int->use_count++;
  1935. mutex_unlock(&serial->shared_int->shared_int_lock);
  1936. }
  1937. if (serial->tiocmget)
  1938. tiocmget_submit_urb(serial,
  1939. serial->tiocmget,
  1940. serial->parent->usb);
  1941. return result;
  1942. }
  1943. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1944. {
  1945. int i;
  1946. struct hso_serial *serial = dev2ser(hso_dev);
  1947. struct hso_tiocmget *tiocmget;
  1948. if (!serial)
  1949. return -ENODEV;
  1950. for (i = 0; i < serial->num_rx_urbs; i++) {
  1951. if (serial->rx_urb[i]) {
  1952. usb_kill_urb(serial->rx_urb[i]);
  1953. serial->rx_urb_filled[i] = 0;
  1954. }
  1955. }
  1956. serial->curr_rx_urb_idx = 0;
  1957. serial->curr_rx_urb_offset = 0;
  1958. if (serial->tx_urb)
  1959. usb_kill_urb(serial->tx_urb);
  1960. if (serial->shared_int) {
  1961. mutex_lock(&serial->shared_int->shared_int_lock);
  1962. if (serial->shared_int->use_count &&
  1963. (--serial->shared_int->use_count == 0)) {
  1964. struct urb *urb;
  1965. urb = serial->shared_int->shared_intr_urb;
  1966. if (urb)
  1967. usb_kill_urb(urb);
  1968. }
  1969. mutex_unlock(&serial->shared_int->shared_int_lock);
  1970. }
  1971. tiocmget = serial->tiocmget;
  1972. if (tiocmget) {
  1973. wake_up_interruptible(&tiocmget->waitq);
  1974. usb_kill_urb(tiocmget->urb);
  1975. }
  1976. return 0;
  1977. }
  1978. static void hso_serial_common_free(struct hso_serial *serial)
  1979. {
  1980. int i;
  1981. if (serial->parent->dev)
  1982. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1983. tty_unregister_device(tty_drv, serial->minor);
  1984. for (i = 0; i < serial->num_rx_urbs; i++) {
  1985. /* unlink and free RX URB */
  1986. usb_free_urb(serial->rx_urb[i]);
  1987. /* free the RX buffer */
  1988. kfree(serial->rx_data[i]);
  1989. }
  1990. /* unlink and free TX URB */
  1991. usb_free_urb(serial->tx_urb);
  1992. kfree(serial->tx_data);
  1993. }
  1994. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1995. int rx_size, int tx_size)
  1996. {
  1997. struct device *dev;
  1998. int minor;
  1999. int i;
  2000. minor = get_free_serial_index();
  2001. if (minor < 0)
  2002. goto exit;
  2003. /* register our minor number */
  2004. serial->parent->dev = tty_register_device(tty_drv, minor,
  2005. &serial->parent->interface->dev);
  2006. dev = serial->parent->dev;
  2007. dev_set_drvdata(dev, serial->parent);
  2008. i = device_create_file(dev, &dev_attr_hsotype);
  2009. /* fill in specific data for later use */
  2010. serial->minor = minor;
  2011. serial->magic = HSO_SERIAL_MAGIC;
  2012. spin_lock_init(&serial->serial_lock);
  2013. serial->num_rx_urbs = num_urbs;
  2014. /* RX, allocate urb and initialize */
  2015. /* prepare our RX buffer */
  2016. serial->rx_data_length = rx_size;
  2017. for (i = 0; i < serial->num_rx_urbs; i++) {
  2018. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2019. if (!serial->rx_urb[i]) {
  2020. dev_err(dev, "Could not allocate urb?\n");
  2021. goto exit;
  2022. }
  2023. serial->rx_urb[i]->transfer_buffer = NULL;
  2024. serial->rx_urb[i]->transfer_buffer_length = 0;
  2025. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2026. GFP_KERNEL);
  2027. if (!serial->rx_data[i]) {
  2028. dev_err(dev, "%s - Out of memory\n", __func__);
  2029. goto exit;
  2030. }
  2031. }
  2032. /* TX, allocate urb and initialize */
  2033. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2034. if (!serial->tx_urb) {
  2035. dev_err(dev, "Could not allocate urb?\n");
  2036. goto exit;
  2037. }
  2038. serial->tx_urb->transfer_buffer = NULL;
  2039. serial->tx_urb->transfer_buffer_length = 0;
  2040. /* prepare our TX buffer */
  2041. serial->tx_data_count = 0;
  2042. serial->tx_buffer_count = 0;
  2043. serial->tx_data_length = tx_size;
  2044. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2045. if (!serial->tx_data) {
  2046. dev_err(dev, "%s - Out of memory", __func__);
  2047. goto exit;
  2048. }
  2049. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2050. if (!serial->tx_buffer) {
  2051. dev_err(dev, "%s - Out of memory", __func__);
  2052. goto exit;
  2053. }
  2054. return 0;
  2055. exit:
  2056. hso_serial_common_free(serial);
  2057. return -1;
  2058. }
  2059. /* Creates a general hso device */
  2060. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2061. int port_spec)
  2062. {
  2063. struct hso_device *hso_dev;
  2064. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2065. if (!hso_dev)
  2066. return NULL;
  2067. hso_dev->port_spec = port_spec;
  2068. hso_dev->usb = interface_to_usbdev(intf);
  2069. hso_dev->interface = intf;
  2070. kref_init(&hso_dev->ref);
  2071. mutex_init(&hso_dev->mutex);
  2072. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2073. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2074. return hso_dev;
  2075. }
  2076. /* Removes a network device in the network device table */
  2077. static int remove_net_device(struct hso_device *hso_dev)
  2078. {
  2079. int i;
  2080. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2081. if (network_table[i] == hso_dev) {
  2082. network_table[i] = NULL;
  2083. break;
  2084. }
  2085. }
  2086. if (i == HSO_MAX_NET_DEVICES)
  2087. return -1;
  2088. return 0;
  2089. }
  2090. /* Frees our network device */
  2091. static void hso_free_net_device(struct hso_device *hso_dev)
  2092. {
  2093. int i;
  2094. struct hso_net *hso_net = dev2net(hso_dev);
  2095. if (!hso_net)
  2096. return;
  2097. remove_net_device(hso_net->parent);
  2098. if (hso_net->net) {
  2099. unregister_netdev(hso_net->net);
  2100. free_netdev(hso_net->net);
  2101. }
  2102. /* start freeing */
  2103. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2104. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2105. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2106. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2107. }
  2108. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2109. kfree(hso_net->mux_bulk_tx_buf);
  2110. hso_net->mux_bulk_tx_buf = NULL;
  2111. kfree(hso_dev);
  2112. }
  2113. static const struct net_device_ops hso_netdev_ops = {
  2114. .ndo_open = hso_net_open,
  2115. .ndo_stop = hso_net_close,
  2116. .ndo_start_xmit = hso_net_start_xmit,
  2117. .ndo_tx_timeout = hso_net_tx_timeout,
  2118. };
  2119. /* initialize the network interface */
  2120. static void hso_net_init(struct net_device *net)
  2121. {
  2122. struct hso_net *hso_net = netdev_priv(net);
  2123. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2124. /* fill in the other fields */
  2125. net->netdev_ops = &hso_netdev_ops;
  2126. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2127. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2128. net->type = ARPHRD_NONE;
  2129. net->mtu = DEFAULT_MTU - 14;
  2130. net->tx_queue_len = 10;
  2131. SET_ETHTOOL_OPS(net, &ops);
  2132. /* and initialize the semaphore */
  2133. spin_lock_init(&hso_net->net_lock);
  2134. }
  2135. /* Adds a network device in the network device table */
  2136. static int add_net_device(struct hso_device *hso_dev)
  2137. {
  2138. int i;
  2139. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2140. if (network_table[i] == NULL) {
  2141. network_table[i] = hso_dev;
  2142. break;
  2143. }
  2144. }
  2145. if (i == HSO_MAX_NET_DEVICES)
  2146. return -1;
  2147. return 0;
  2148. }
  2149. static int hso_rfkill_set_block(void *data, bool blocked)
  2150. {
  2151. struct hso_device *hso_dev = data;
  2152. int enabled = !blocked;
  2153. int rv;
  2154. mutex_lock(&hso_dev->mutex);
  2155. if (hso_dev->usb_gone)
  2156. rv = 0;
  2157. else
  2158. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2159. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2160. USB_CTRL_SET_TIMEOUT);
  2161. mutex_unlock(&hso_dev->mutex);
  2162. return rv;
  2163. }
  2164. static const struct rfkill_ops hso_rfkill_ops = {
  2165. .set_block = hso_rfkill_set_block,
  2166. };
  2167. /* Creates and sets up everything for rfkill */
  2168. static void hso_create_rfkill(struct hso_device *hso_dev,
  2169. struct usb_interface *interface)
  2170. {
  2171. struct hso_net *hso_net = dev2net(hso_dev);
  2172. struct device *dev = &hso_net->net->dev;
  2173. char *rfkn;
  2174. rfkn = kzalloc(20, GFP_KERNEL);
  2175. if (!rfkn)
  2176. dev_err(dev, "%s - Out of memory\n", __func__);
  2177. snprintf(rfkn, 20, "hso-%d",
  2178. interface->altsetting->desc.bInterfaceNumber);
  2179. hso_net->rfkill = rfkill_alloc(rfkn,
  2180. &interface_to_usbdev(interface)->dev,
  2181. RFKILL_TYPE_WWAN,
  2182. &hso_rfkill_ops, hso_dev);
  2183. if (!hso_net->rfkill) {
  2184. dev_err(dev, "%s - Out of memory\n", __func__);
  2185. kfree(rfkn);
  2186. return;
  2187. }
  2188. if (rfkill_register(hso_net->rfkill) < 0) {
  2189. rfkill_destroy(hso_net->rfkill);
  2190. kfree(rfkn);
  2191. hso_net->rfkill = NULL;
  2192. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2193. return;
  2194. }
  2195. }
  2196. static struct device_type hso_type = {
  2197. .name = "wwan",
  2198. };
  2199. /* Creates our network device */
  2200. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2201. int port_spec)
  2202. {
  2203. int result, i;
  2204. struct net_device *net;
  2205. struct hso_net *hso_net;
  2206. struct hso_device *hso_dev;
  2207. hso_dev = hso_create_device(interface, port_spec);
  2208. if (!hso_dev)
  2209. return NULL;
  2210. /* allocate our network device, then we can put in our private data */
  2211. /* call hso_net_init to do the basic initialization */
  2212. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2213. if (!net) {
  2214. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2215. goto exit;
  2216. }
  2217. hso_net = netdev_priv(net);
  2218. hso_dev->port_data.dev_net = hso_net;
  2219. hso_net->net = net;
  2220. hso_net->parent = hso_dev;
  2221. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2222. USB_DIR_IN);
  2223. if (!hso_net->in_endp) {
  2224. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2225. goto exit;
  2226. }
  2227. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2228. USB_DIR_OUT);
  2229. if (!hso_net->out_endp) {
  2230. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2231. goto exit;
  2232. }
  2233. SET_NETDEV_DEV(net, &interface->dev);
  2234. SET_NETDEV_DEVTYPE(net, &hso_type);
  2235. /* registering our net device */
  2236. result = register_netdev(net);
  2237. if (result) {
  2238. dev_err(&interface->dev, "Failed to register device\n");
  2239. goto exit;
  2240. }
  2241. /* start allocating */
  2242. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2243. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2244. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2245. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2246. goto exit;
  2247. }
  2248. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2249. GFP_KERNEL);
  2250. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2251. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2252. goto exit;
  2253. }
  2254. }
  2255. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2256. if (!hso_net->mux_bulk_tx_urb) {
  2257. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2258. goto exit;
  2259. }
  2260. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2261. if (!hso_net->mux_bulk_tx_buf) {
  2262. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2263. goto exit;
  2264. }
  2265. add_net_device(hso_dev);
  2266. hso_log_port(hso_dev);
  2267. hso_create_rfkill(hso_dev, interface);
  2268. return hso_dev;
  2269. exit:
  2270. hso_free_net_device(hso_dev);
  2271. return NULL;
  2272. }
  2273. static void hso_free_tiomget(struct hso_serial *serial)
  2274. {
  2275. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2276. if (tiocmget) {
  2277. if (tiocmget->urb) {
  2278. usb_free_urb(tiocmget->urb);
  2279. tiocmget->urb = NULL;
  2280. }
  2281. serial->tiocmget = NULL;
  2282. kfree(tiocmget);
  2283. }
  2284. }
  2285. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2286. static void hso_free_serial_device(struct hso_device *hso_dev)
  2287. {
  2288. struct hso_serial *serial = dev2ser(hso_dev);
  2289. if (!serial)
  2290. return;
  2291. set_serial_by_index(serial->minor, NULL);
  2292. hso_serial_common_free(serial);
  2293. if (serial->shared_int) {
  2294. mutex_lock(&serial->shared_int->shared_int_lock);
  2295. if (--serial->shared_int->ref_count == 0)
  2296. hso_free_shared_int(serial->shared_int);
  2297. else
  2298. mutex_unlock(&serial->shared_int->shared_int_lock);
  2299. }
  2300. hso_free_tiomget(serial);
  2301. kfree(serial);
  2302. kfree(hso_dev);
  2303. }
  2304. /* Creates a bulk AT channel */
  2305. static struct hso_device *hso_create_bulk_serial_device(
  2306. struct usb_interface *interface, int port)
  2307. {
  2308. struct hso_device *hso_dev;
  2309. struct hso_serial *serial;
  2310. int num_urbs;
  2311. struct hso_tiocmget *tiocmget;
  2312. hso_dev = hso_create_device(interface, port);
  2313. if (!hso_dev)
  2314. return NULL;
  2315. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2316. if (!serial)
  2317. goto exit;
  2318. serial->parent = hso_dev;
  2319. hso_dev->port_data.dev_serial = serial;
  2320. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2321. num_urbs = 2;
  2322. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2323. GFP_KERNEL);
  2324. /* it isn't going to break our heart if serial->tiocmget
  2325. * allocation fails don't bother checking this.
  2326. */
  2327. if (serial->tiocmget) {
  2328. tiocmget = serial->tiocmget;
  2329. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2330. if (tiocmget->urb) {
  2331. mutex_init(&tiocmget->mutex);
  2332. init_waitqueue_head(&tiocmget->waitq);
  2333. tiocmget->endp = hso_get_ep(
  2334. interface,
  2335. USB_ENDPOINT_XFER_INT,
  2336. USB_DIR_IN);
  2337. } else
  2338. hso_free_tiomget(serial);
  2339. }
  2340. }
  2341. else
  2342. num_urbs = 1;
  2343. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2344. BULK_URB_TX_SIZE))
  2345. goto exit;
  2346. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2347. USB_DIR_IN);
  2348. if (!serial->in_endp) {
  2349. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2350. goto exit2;
  2351. }
  2352. if (!
  2353. (serial->out_endp =
  2354. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2355. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2356. goto exit2;
  2357. }
  2358. serial->write_data = hso_std_serial_write_data;
  2359. /* and record this serial */
  2360. set_serial_by_index(serial->minor, serial);
  2361. /* setup the proc dirs and files if needed */
  2362. hso_log_port(hso_dev);
  2363. /* done, return it */
  2364. return hso_dev;
  2365. exit2:
  2366. hso_serial_common_free(serial);
  2367. exit:
  2368. hso_free_tiomget(serial);
  2369. kfree(serial);
  2370. kfree(hso_dev);
  2371. return NULL;
  2372. }
  2373. /* Creates a multiplexed AT channel */
  2374. static
  2375. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2376. int port,
  2377. struct hso_shared_int *mux)
  2378. {
  2379. struct hso_device *hso_dev;
  2380. struct hso_serial *serial;
  2381. int port_spec;
  2382. port_spec = HSO_INTF_MUX;
  2383. port_spec &= ~HSO_PORT_MASK;
  2384. port_spec |= hso_mux_to_port(port);
  2385. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2386. return NULL;
  2387. hso_dev = hso_create_device(interface, port_spec);
  2388. if (!hso_dev)
  2389. return NULL;
  2390. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2391. if (!serial)
  2392. goto exit;
  2393. hso_dev->port_data.dev_serial = serial;
  2394. serial->parent = hso_dev;
  2395. if (hso_serial_common_create
  2396. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2397. goto exit;
  2398. serial->tx_data_length--;
  2399. serial->write_data = hso_mux_serial_write_data;
  2400. serial->shared_int = mux;
  2401. mutex_lock(&serial->shared_int->shared_int_lock);
  2402. serial->shared_int->ref_count++;
  2403. mutex_unlock(&serial->shared_int->shared_int_lock);
  2404. /* and record this serial */
  2405. set_serial_by_index(serial->minor, serial);
  2406. /* setup the proc dirs and files if needed */
  2407. hso_log_port(hso_dev);
  2408. /* done, return it */
  2409. return hso_dev;
  2410. exit:
  2411. if (serial) {
  2412. tty_unregister_device(tty_drv, serial->minor);
  2413. kfree(serial);
  2414. }
  2415. if (hso_dev)
  2416. kfree(hso_dev);
  2417. return NULL;
  2418. }
  2419. static void hso_free_shared_int(struct hso_shared_int *mux)
  2420. {
  2421. usb_free_urb(mux->shared_intr_urb);
  2422. kfree(mux->shared_intr_buf);
  2423. mutex_unlock(&mux->shared_int_lock);
  2424. kfree(mux);
  2425. }
  2426. static
  2427. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2428. {
  2429. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2430. if (!mux)
  2431. return NULL;
  2432. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2433. USB_DIR_IN);
  2434. if (!mux->intr_endp) {
  2435. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2436. goto exit;
  2437. }
  2438. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2439. if (!mux->shared_intr_urb) {
  2440. dev_err(&interface->dev, "Could not allocate intr urb?");
  2441. goto exit;
  2442. }
  2443. mux->shared_intr_buf =
  2444. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2445. GFP_KERNEL);
  2446. if (!mux->shared_intr_buf) {
  2447. dev_err(&interface->dev, "Could not allocate intr buf?");
  2448. goto exit;
  2449. }
  2450. mutex_init(&mux->shared_int_lock);
  2451. return mux;
  2452. exit:
  2453. kfree(mux->shared_intr_buf);
  2454. usb_free_urb(mux->shared_intr_urb);
  2455. kfree(mux);
  2456. return NULL;
  2457. }
  2458. /* Gets the port spec for a certain interface */
  2459. static int hso_get_config_data(struct usb_interface *interface)
  2460. {
  2461. struct usb_device *usbdev = interface_to_usbdev(interface);
  2462. u8 config_data[17];
  2463. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2464. s32 result;
  2465. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2466. 0x86, 0xC0, 0, 0, config_data, 17,
  2467. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2468. return -EIO;
  2469. }
  2470. switch (config_data[if_num]) {
  2471. case 0x0:
  2472. result = 0;
  2473. break;
  2474. case 0x1:
  2475. result = HSO_PORT_DIAG;
  2476. break;
  2477. case 0x2:
  2478. result = HSO_PORT_GPS;
  2479. break;
  2480. case 0x3:
  2481. result = HSO_PORT_GPS_CONTROL;
  2482. break;
  2483. case 0x4:
  2484. result = HSO_PORT_APP;
  2485. break;
  2486. case 0x5:
  2487. result = HSO_PORT_APP2;
  2488. break;
  2489. case 0x6:
  2490. result = HSO_PORT_CONTROL;
  2491. break;
  2492. case 0x7:
  2493. result = HSO_PORT_NETWORK;
  2494. break;
  2495. case 0x8:
  2496. result = HSO_PORT_MODEM;
  2497. break;
  2498. case 0x9:
  2499. result = HSO_PORT_MSD;
  2500. break;
  2501. case 0xa:
  2502. result = HSO_PORT_PCSC;
  2503. break;
  2504. case 0xb:
  2505. result = HSO_PORT_VOICE;
  2506. break;
  2507. default:
  2508. result = 0;
  2509. }
  2510. if (result)
  2511. result |= HSO_INTF_BULK;
  2512. if (config_data[16] & 0x1)
  2513. result |= HSO_INFO_CRC_BUG;
  2514. return result;
  2515. }
  2516. /* called once for each interface upon device insertion */
  2517. static int hso_probe(struct usb_interface *interface,
  2518. const struct usb_device_id *id)
  2519. {
  2520. int mux, i, if_num, port_spec;
  2521. unsigned char port_mask;
  2522. struct hso_device *hso_dev = NULL;
  2523. struct hso_shared_int *shared_int;
  2524. struct hso_device *tmp_dev = NULL;
  2525. if_num = interface->altsetting->desc.bInterfaceNumber;
  2526. /* Get the interface/port specification from either driver_info or from
  2527. * the device itself */
  2528. if (id->driver_info)
  2529. port_spec = ((u32 *)(id->driver_info))[if_num];
  2530. else
  2531. port_spec = hso_get_config_data(interface);
  2532. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2533. dev_err(&interface->dev, "Not our interface\n");
  2534. return -ENODEV;
  2535. }
  2536. /* Check if we need to switch to alt interfaces prior to port
  2537. * configuration */
  2538. if (interface->num_altsetting > 1)
  2539. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2540. interface->needs_remote_wakeup = 1;
  2541. /* Allocate new hso device(s) */
  2542. switch (port_spec & HSO_INTF_MASK) {
  2543. case HSO_INTF_MUX:
  2544. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2545. /* Create the network device */
  2546. if (!disable_net) {
  2547. hso_dev = hso_create_net_device(interface,
  2548. port_spec);
  2549. if (!hso_dev)
  2550. goto exit;
  2551. tmp_dev = hso_dev;
  2552. }
  2553. }
  2554. if (hso_get_mux_ports(interface, &port_mask))
  2555. /* TODO: de-allocate everything */
  2556. goto exit;
  2557. shared_int = hso_create_shared_int(interface);
  2558. if (!shared_int)
  2559. goto exit;
  2560. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2561. if (port_mask & i) {
  2562. hso_dev = hso_create_mux_serial_device(
  2563. interface, i, shared_int);
  2564. if (!hso_dev)
  2565. goto exit;
  2566. }
  2567. }
  2568. if (tmp_dev)
  2569. hso_dev = tmp_dev;
  2570. break;
  2571. case HSO_INTF_BULK:
  2572. /* It's a regular bulk interface */
  2573. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) &&
  2574. !disable_net)
  2575. hso_dev = hso_create_net_device(interface, port_spec);
  2576. else
  2577. hso_dev =
  2578. hso_create_bulk_serial_device(interface, port_spec);
  2579. if (!hso_dev)
  2580. goto exit;
  2581. break;
  2582. default:
  2583. goto exit;
  2584. }
  2585. /* save our data pointer in this device */
  2586. usb_set_intfdata(interface, hso_dev);
  2587. /* done */
  2588. return 0;
  2589. exit:
  2590. hso_free_interface(interface);
  2591. return -ENODEV;
  2592. }
  2593. /* device removed, cleaning up */
  2594. static void hso_disconnect(struct usb_interface *interface)
  2595. {
  2596. hso_free_interface(interface);
  2597. /* remove reference of our private data */
  2598. usb_set_intfdata(interface, NULL);
  2599. }
  2600. static void async_get_intf(struct work_struct *data)
  2601. {
  2602. struct hso_device *hso_dev =
  2603. container_of(data, struct hso_device, async_get_intf);
  2604. usb_autopm_get_interface(hso_dev->interface);
  2605. }
  2606. static void async_put_intf(struct work_struct *data)
  2607. {
  2608. struct hso_device *hso_dev =
  2609. container_of(data, struct hso_device, async_put_intf);
  2610. usb_autopm_put_interface(hso_dev->interface);
  2611. }
  2612. static int hso_get_activity(struct hso_device *hso_dev)
  2613. {
  2614. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2615. if (!hso_dev->is_active) {
  2616. hso_dev->is_active = 1;
  2617. schedule_work(&hso_dev->async_get_intf);
  2618. }
  2619. }
  2620. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2621. return -EAGAIN;
  2622. usb_mark_last_busy(hso_dev->usb);
  2623. return 0;
  2624. }
  2625. static int hso_put_activity(struct hso_device *hso_dev)
  2626. {
  2627. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2628. if (hso_dev->is_active) {
  2629. hso_dev->is_active = 0;
  2630. schedule_work(&hso_dev->async_put_intf);
  2631. return -EAGAIN;
  2632. }
  2633. }
  2634. hso_dev->is_active = 0;
  2635. return 0;
  2636. }
  2637. /* called by kernel when we need to suspend device */
  2638. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2639. {
  2640. int i, result;
  2641. /* Stop all serial ports */
  2642. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2643. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2644. result = hso_stop_serial_device(serial_table[i]);
  2645. if (result)
  2646. goto out;
  2647. }
  2648. }
  2649. /* Stop all network ports */
  2650. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2651. if (network_table[i] &&
  2652. (network_table[i]->interface == iface)) {
  2653. result = hso_stop_net_device(network_table[i]);
  2654. if (result)
  2655. goto out;
  2656. }
  2657. }
  2658. out:
  2659. return 0;
  2660. }
  2661. /* called by kernel when we need to resume device */
  2662. static int hso_resume(struct usb_interface *iface)
  2663. {
  2664. int i, result = 0;
  2665. struct hso_net *hso_net;
  2666. /* Start all serial ports */
  2667. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2668. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2669. if (dev2ser(serial_table[i])->open_count) {
  2670. result =
  2671. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2672. hso_kick_transmit(dev2ser(serial_table[i]));
  2673. if (result)
  2674. goto out;
  2675. }
  2676. }
  2677. }
  2678. /* Start all network ports */
  2679. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2680. if (network_table[i] &&
  2681. (network_table[i]->interface == iface)) {
  2682. hso_net = dev2net(network_table[i]);
  2683. if (hso_net->flags & IFF_UP) {
  2684. /* First transmit any lingering data,
  2685. then restart the device. */
  2686. if (hso_net->skb_tx_buf) {
  2687. dev_dbg(&iface->dev,
  2688. "Transmitting"
  2689. " lingering data\n");
  2690. hso_net_start_xmit(hso_net->skb_tx_buf,
  2691. hso_net->net);
  2692. hso_net->skb_tx_buf = NULL;
  2693. }
  2694. result = hso_start_net_device(network_table[i]);
  2695. if (result)
  2696. goto out;
  2697. }
  2698. }
  2699. }
  2700. out:
  2701. return result;
  2702. }
  2703. static void hso_serial_ref_free(struct kref *ref)
  2704. {
  2705. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2706. hso_free_serial_device(hso_dev);
  2707. }
  2708. static void hso_free_interface(struct usb_interface *interface)
  2709. {
  2710. struct hso_serial *hso_dev;
  2711. struct tty_struct *tty;
  2712. int i;
  2713. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2714. if (serial_table[i] &&
  2715. (serial_table[i]->interface == interface)) {
  2716. hso_dev = dev2ser(serial_table[i]);
  2717. spin_lock_irq(&hso_dev->serial_lock);
  2718. tty = tty_kref_get(hso_dev->tty);
  2719. spin_unlock_irq(&hso_dev->serial_lock);
  2720. if (tty)
  2721. tty_hangup(tty);
  2722. mutex_lock(&hso_dev->parent->mutex);
  2723. tty_kref_put(tty);
  2724. hso_dev->parent->usb_gone = 1;
  2725. mutex_unlock(&hso_dev->parent->mutex);
  2726. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2727. }
  2728. }
  2729. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2730. if (network_table[i] &&
  2731. (network_table[i]->interface == interface)) {
  2732. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2733. /* hso_stop_net_device doesn't stop the net queue since
  2734. * traffic needs to start it again when suspended */
  2735. netif_stop_queue(dev2net(network_table[i])->net);
  2736. hso_stop_net_device(network_table[i]);
  2737. cancel_work_sync(&network_table[i]->async_put_intf);
  2738. cancel_work_sync(&network_table[i]->async_get_intf);
  2739. if (rfk) {
  2740. rfkill_unregister(rfk);
  2741. rfkill_destroy(rfk);
  2742. }
  2743. hso_free_net_device(network_table[i]);
  2744. }
  2745. }
  2746. }
  2747. /* Helper functions */
  2748. /* Get the endpoint ! */
  2749. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2750. int type, int dir)
  2751. {
  2752. int i;
  2753. struct usb_host_interface *iface = intf->cur_altsetting;
  2754. struct usb_endpoint_descriptor *endp;
  2755. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2756. endp = &iface->endpoint[i].desc;
  2757. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2758. (usb_endpoint_type(endp) == type))
  2759. return endp;
  2760. }
  2761. return NULL;
  2762. }
  2763. /* Get the byte that describes which ports are enabled */
  2764. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2765. {
  2766. int i;
  2767. struct usb_host_interface *iface = intf->cur_altsetting;
  2768. if (iface->extralen == 3) {
  2769. *ports = iface->extra[2];
  2770. return 0;
  2771. }
  2772. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2773. if (iface->endpoint[i].extralen == 3) {
  2774. *ports = iface->endpoint[i].extra[2];
  2775. return 0;
  2776. }
  2777. }
  2778. return -1;
  2779. }
  2780. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2781. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2782. struct usb_device *usb, gfp_t gfp)
  2783. {
  2784. int result;
  2785. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2786. usb_rcvintpipe(usb,
  2787. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2788. shared_int->shared_intr_buf,
  2789. 1,
  2790. intr_callback, shared_int,
  2791. shared_int->intr_endp->bInterval);
  2792. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2793. if (result)
  2794. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2795. result);
  2796. return result;
  2797. }
  2798. /* operations setup of the serial interface */
  2799. static const struct tty_operations hso_serial_ops = {
  2800. .open = hso_serial_open,
  2801. .close = hso_serial_close,
  2802. .write = hso_serial_write,
  2803. .write_room = hso_serial_write_room,
  2804. .ioctl = hso_serial_ioctl,
  2805. .set_termios = hso_serial_set_termios,
  2806. .chars_in_buffer = hso_serial_chars_in_buffer,
  2807. .tiocmget = hso_serial_tiocmget,
  2808. .tiocmset = hso_serial_tiocmset,
  2809. .unthrottle = hso_unthrottle
  2810. };
  2811. static struct usb_driver hso_driver = {
  2812. .name = driver_name,
  2813. .probe = hso_probe,
  2814. .disconnect = hso_disconnect,
  2815. .id_table = hso_ids,
  2816. .suspend = hso_suspend,
  2817. .resume = hso_resume,
  2818. .reset_resume = hso_resume,
  2819. .supports_autosuspend = 1,
  2820. };
  2821. static int __init hso_init(void)
  2822. {
  2823. int i;
  2824. int result;
  2825. /* put it in the log */
  2826. printk(KERN_INFO "hso: %s\n", version);
  2827. /* Initialise the serial table semaphore and table */
  2828. spin_lock_init(&serial_table_lock);
  2829. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2830. serial_table[i] = NULL;
  2831. /* allocate our driver using the proper amount of supported minors */
  2832. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2833. if (!tty_drv)
  2834. return -ENOMEM;
  2835. /* fill in all needed values */
  2836. tty_drv->magic = TTY_DRIVER_MAGIC;
  2837. tty_drv->owner = THIS_MODULE;
  2838. tty_drv->driver_name = driver_name;
  2839. tty_drv->name = tty_filename;
  2840. /* if major number is provided as parameter, use that one */
  2841. if (tty_major)
  2842. tty_drv->major = tty_major;
  2843. tty_drv->minor_start = 0;
  2844. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2845. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2846. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2847. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2848. tty_drv->init_termios = tty_std_termios;
  2849. hso_init_termios(&tty_drv->init_termios);
  2850. tty_set_operations(tty_drv, &hso_serial_ops);
  2851. /* register the tty driver */
  2852. result = tty_register_driver(tty_drv);
  2853. if (result) {
  2854. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2855. __func__, result);
  2856. return result;
  2857. }
  2858. /* register this module as an usb driver */
  2859. result = usb_register(&hso_driver);
  2860. if (result) {
  2861. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2862. result);
  2863. /* cleanup serial interface */
  2864. tty_unregister_driver(tty_drv);
  2865. return result;
  2866. }
  2867. /* done */
  2868. return 0;
  2869. }
  2870. static void __exit hso_exit(void)
  2871. {
  2872. printk(KERN_INFO "hso: unloaded\n");
  2873. tty_unregister_driver(tty_drv);
  2874. /* deregister the usb driver */
  2875. usb_deregister(&hso_driver);
  2876. }
  2877. /* Module definitions */
  2878. module_init(hso_init);
  2879. module_exit(hso_exit);
  2880. MODULE_AUTHOR(MOD_AUTHOR);
  2881. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2882. MODULE_LICENSE(MOD_LICENSE);
  2883. MODULE_INFO(Version, DRIVER_VERSION);
  2884. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2885. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2886. module_param(debug, int, S_IRUGO | S_IWUSR);
  2887. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2888. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2889. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2890. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2891. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2892. module_param(disable_net, int, S_IRUGO | S_IWUSR);